DEV Community

Discussion on: Introduction: from function to closure

dhkamp profile image
David Hölkeskamp • Edited on

Nice read - I like it.
I've got one question regarding performance and your anonymous function inside your debounce function.
Will the engine (e.g. V8) remember your function internals or will it create the anonymous function, inside it's function storage, everytime debounce gets called?

stereobooster profile image
stereobooster Author

Every time you call const df = debounce(f) it will create new closure, which contains inside of it anonymous function (return x => {) which will be returned (and assigned to df in this case). But after this you use df which is one instance of the function. If you call const df2 = debounce(f) it will create another closure and another anonymous function.

It works the same way as if you would assign variable. Consider following code example const test = () => [] it will create new instance of array every time you call it (test() !== test()).

Did I answer your question?

dhkamp profile image
David Hölkeskamp

Definately thank you - just thought there would be some kind of optimization going on inside the engine like memoization.