4 ms·
Of course, you could allocate once by extracting it or with an IIFE + closure from TFA's functions. lookup = (() => { map = { a: 1, b: 2 } r
by hombre_fatal 8y ago
Of course, you could allocate once by extracting it or with an IIFE + closure from TFA's functions.
lookup = (() => {
map = { a: 1, b: 2 }
return (key) => map[key] || 'not found'
})()
- bastawhiz 8y agoThat helps solve the performance issue within one execution of the surrounding function, but if the surrounding function is called many times, you're more introducing more closures and more indirection. Switch statements involve no allocation. Closures objects, and function calls aren't free.
- hombre_fatal 8y agoWell, in my example, the surrounding function is instantly invoked once and cannot be invoked again. It's merely a useful pattern to avoid polluting the top-level with extraction.