what is the lifetime of javascript anonymous function?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












If I write this in global scope:



(function())();


is the anonymous function created when the statement is executed and destroyed immediately after the statement is executed?



if I write this in a function:



function foo()

var a=1;
(function())();
a++;



Does the anonymous function exist until foo returns, or just exist during the execution of that statement?










share|improve this question





















  • My first guess is that it get destroyed after the statement is executed by the garbage collector. Because there is no link kept to that piece of code afterwards. I'll wait for answers with proof :) Look in here
    – Grégory NEUT
    2 hours ago







  • 1




    Yes, functions are created and garbage-collected like any other objects.
    – Bergi
    2 hours ago










  • Great question! I'd never thought to ask it but now I'm really curious to know the answer.
    – Rocky Sims
    2 hours ago














up vote
6
down vote

favorite












If I write this in global scope:



(function())();


is the anonymous function created when the statement is executed and destroyed immediately after the statement is executed?



if I write this in a function:



function foo()

var a=1;
(function())();
a++;



Does the anonymous function exist until foo returns, or just exist during the execution of that statement?










share|improve this question





















  • My first guess is that it get destroyed after the statement is executed by the garbage collector. Because there is no link kept to that piece of code afterwards. I'll wait for answers with proof :) Look in here
    – Grégory NEUT
    2 hours ago







  • 1




    Yes, functions are created and garbage-collected like any other objects.
    – Bergi
    2 hours ago










  • Great question! I'd never thought to ask it but now I'm really curious to know the answer.
    – Rocky Sims
    2 hours ago












up vote
6
down vote

favorite









up vote
6
down vote

favorite











If I write this in global scope:



(function())();


is the anonymous function created when the statement is executed and destroyed immediately after the statement is executed?



if I write this in a function:



function foo()

var a=1;
(function())();
a++;



Does the anonymous function exist until foo returns, or just exist during the execution of that statement?










share|improve this question













If I write this in global scope:



(function())();


is the anonymous function created when the statement is executed and destroyed immediately after the statement is executed?



if I write this in a function:



function foo()

var a=1;
(function())();
a++;



Does the anonymous function exist until foo returns, or just exist during the execution of that statement?







javascript anonymous-function lifetime






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









William

312




312











  • My first guess is that it get destroyed after the statement is executed by the garbage collector. Because there is no link kept to that piece of code afterwards. I'll wait for answers with proof :) Look in here
    – Grégory NEUT
    2 hours ago







  • 1




    Yes, functions are created and garbage-collected like any other objects.
    – Bergi
    2 hours ago










  • Great question! I'd never thought to ask it but now I'm really curious to know the answer.
    – Rocky Sims
    2 hours ago
















  • My first guess is that it get destroyed after the statement is executed by the garbage collector. Because there is no link kept to that piece of code afterwards. I'll wait for answers with proof :) Look in here
    – Grégory NEUT
    2 hours ago







  • 1




    Yes, functions are created and garbage-collected like any other objects.
    – Bergi
    2 hours ago










  • Great question! I'd never thought to ask it but now I'm really curious to know the answer.
    – Rocky Sims
    2 hours ago















My first guess is that it get destroyed after the statement is executed by the garbage collector. Because there is no link kept to that piece of code afterwards. I'll wait for answers with proof :) Look in here
– Grégory NEUT
2 hours ago





My first guess is that it get destroyed after the statement is executed by the garbage collector. Because there is no link kept to that piece of code afterwards. I'll wait for answers with proof :) Look in here
– Grégory NEUT
2 hours ago





1




1




Yes, functions are created and garbage-collected like any other objects.
– Bergi
2 hours ago




Yes, functions are created and garbage-collected like any other objects.
– Bergi
2 hours ago












Great question! I'd never thought to ask it but now I'm really curious to know the answer.
– Rocky Sims
2 hours ago




Great question! I'd never thought to ask it but now I'm really curious to know the answer.
– Rocky Sims
2 hours ago












1 Answer
1






active

oldest

votes

















up vote
7
down vote













In this particular case, most engines will optimize that function entirely away, because it does not do anything.



But let's assume that function contains code and is indeed executed. In this case, the function will exist all the time, either as compiled code, as bytecode or as AST for the interpreter.



The part that won't exist all the time is the scope and the possible created closure. The scope created for that function and the closure will only exist as long as the function is executed or a reference to the function with a specific bound scope/closure exists.



So the combination function reference + scope will be allocated at the time the statement (function())(); is executed, and can be released after that statement. But the compiled version of function() might still exist in memory for later use.



For engines that do just in time compiling and optimization, a function might even exist in different compiled versions.



The JIT+optimizer part of modern js engines is a complex topic, a rough description of v8 can be found here html5rocks: JavaScript Compilation:




In V8, the Full compiler runs on all code, and starts executing code as soon as possible, quickly generating good but not great code. This compiler assumes almost nothing about types at compilation time - it expects that types of variables can and will change at runtime.



In parallel with the full compiler, V8 re-compiles "hot" functions (that is, functions that are run many times) with an optimizing compiler. [...] In the optimizing compiler, operations get speculatively inlined (directly placed where they are called). This speeds execution (at the cost of memory footprint), but also enables other optimizations.




Therefore it may be that the generated code has hardly any similarities to the original one.



So a immediately-invoked function expression might even be completely optimized away using inlining.






share|improve this answer






















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52812131%2fwhat-is-the-lifetime-of-javascript-anonymous-function%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    In this particular case, most engines will optimize that function entirely away, because it does not do anything.



    But let's assume that function contains code and is indeed executed. In this case, the function will exist all the time, either as compiled code, as bytecode or as AST for the interpreter.



    The part that won't exist all the time is the scope and the possible created closure. The scope created for that function and the closure will only exist as long as the function is executed or a reference to the function with a specific bound scope/closure exists.



    So the combination function reference + scope will be allocated at the time the statement (function())(); is executed, and can be released after that statement. But the compiled version of function() might still exist in memory for later use.



    For engines that do just in time compiling and optimization, a function might even exist in different compiled versions.



    The JIT+optimizer part of modern js engines is a complex topic, a rough description of v8 can be found here html5rocks: JavaScript Compilation:




    In V8, the Full compiler runs on all code, and starts executing code as soon as possible, quickly generating good but not great code. This compiler assumes almost nothing about types at compilation time - it expects that types of variables can and will change at runtime.



    In parallel with the full compiler, V8 re-compiles "hot" functions (that is, functions that are run many times) with an optimizing compiler. [...] In the optimizing compiler, operations get speculatively inlined (directly placed where they are called). This speeds execution (at the cost of memory footprint), but also enables other optimizations.




    Therefore it may be that the generated code has hardly any similarities to the original one.



    So a immediately-invoked function expression might even be completely optimized away using inlining.






    share|improve this answer


























      up vote
      7
      down vote













      In this particular case, most engines will optimize that function entirely away, because it does not do anything.



      But let's assume that function contains code and is indeed executed. In this case, the function will exist all the time, either as compiled code, as bytecode or as AST for the interpreter.



      The part that won't exist all the time is the scope and the possible created closure. The scope created for that function and the closure will only exist as long as the function is executed or a reference to the function with a specific bound scope/closure exists.



      So the combination function reference + scope will be allocated at the time the statement (function())(); is executed, and can be released after that statement. But the compiled version of function() might still exist in memory for later use.



      For engines that do just in time compiling and optimization, a function might even exist in different compiled versions.



      The JIT+optimizer part of modern js engines is a complex topic, a rough description of v8 can be found here html5rocks: JavaScript Compilation:




      In V8, the Full compiler runs on all code, and starts executing code as soon as possible, quickly generating good but not great code. This compiler assumes almost nothing about types at compilation time - it expects that types of variables can and will change at runtime.



      In parallel with the full compiler, V8 re-compiles "hot" functions (that is, functions that are run many times) with an optimizing compiler. [...] In the optimizing compiler, operations get speculatively inlined (directly placed where they are called). This speeds execution (at the cost of memory footprint), but also enables other optimizations.




      Therefore it may be that the generated code has hardly any similarities to the original one.



      So a immediately-invoked function expression might even be completely optimized away using inlining.






      share|improve this answer
























        up vote
        7
        down vote










        up vote
        7
        down vote









        In this particular case, most engines will optimize that function entirely away, because it does not do anything.



        But let's assume that function contains code and is indeed executed. In this case, the function will exist all the time, either as compiled code, as bytecode or as AST for the interpreter.



        The part that won't exist all the time is the scope and the possible created closure. The scope created for that function and the closure will only exist as long as the function is executed or a reference to the function with a specific bound scope/closure exists.



        So the combination function reference + scope will be allocated at the time the statement (function())(); is executed, and can be released after that statement. But the compiled version of function() might still exist in memory for later use.



        For engines that do just in time compiling and optimization, a function might even exist in different compiled versions.



        The JIT+optimizer part of modern js engines is a complex topic, a rough description of v8 can be found here html5rocks: JavaScript Compilation:




        In V8, the Full compiler runs on all code, and starts executing code as soon as possible, quickly generating good but not great code. This compiler assumes almost nothing about types at compilation time - it expects that types of variables can and will change at runtime.



        In parallel with the full compiler, V8 re-compiles "hot" functions (that is, functions that are run many times) with an optimizing compiler. [...] In the optimizing compiler, operations get speculatively inlined (directly placed where they are called). This speeds execution (at the cost of memory footprint), but also enables other optimizations.




        Therefore it may be that the generated code has hardly any similarities to the original one.



        So a immediately-invoked function expression might even be completely optimized away using inlining.






        share|improve this answer














        In this particular case, most engines will optimize that function entirely away, because it does not do anything.



        But let's assume that function contains code and is indeed executed. In this case, the function will exist all the time, either as compiled code, as bytecode or as AST for the interpreter.



        The part that won't exist all the time is the scope and the possible created closure. The scope created for that function and the closure will only exist as long as the function is executed or a reference to the function with a specific bound scope/closure exists.



        So the combination function reference + scope will be allocated at the time the statement (function())(); is executed, and can be released after that statement. But the compiled version of function() might still exist in memory for later use.



        For engines that do just in time compiling and optimization, a function might even exist in different compiled versions.



        The JIT+optimizer part of modern js engines is a complex topic, a rough description of v8 can be found here html5rocks: JavaScript Compilation:




        In V8, the Full compiler runs on all code, and starts executing code as soon as possible, quickly generating good but not great code. This compiler assumes almost nothing about types at compilation time - it expects that types of variables can and will change at runtime.



        In parallel with the full compiler, V8 re-compiles "hot" functions (that is, functions that are run many times) with an optimizing compiler. [...] In the optimizing compiler, operations get speculatively inlined (directly placed where they are called). This speeds execution (at the cost of memory footprint), but also enables other optimizations.




        Therefore it may be that the generated code has hardly any similarities to the original one.



        So a immediately-invoked function expression might even be completely optimized away using inlining.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 2 hours ago









        t.niese

        20.1k63562




        20.1k63562



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52812131%2fwhat-is-the-lifetime-of-javascript-anonymous-function%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery