DEV Community

Cover image for Introduction: from function to closure

Introduction: from function to closure

stereobooster on June 26, 2019

Minimal introduction: from theoretical concept of function to practical usage of closures in programming. I show the same concept from different po...
Collapse
 
dhkamp profile image
David Hölkeskamp • Edited

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?

Collapse
 
stereobooster profile image
stereobooster

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?

Collapse
 
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.

Collapse
 
ganonbit profile image
Andrew Reese

Saving this for the next jr dev I have under me, really nicely illustrated way to explain each step. Thanks!