what is the lifetime of javascript anonymous function?
Clash 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?
javascript anonymous-function lifetime
add a comment |Â
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?
javascript anonymous-function lifetime
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
add a comment |Â
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?
javascript anonymous-function lifetime
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
javascript anonymous-function lifetime
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 1 hour ago
answered 2 hours ago
t.niese
20.1k63562
20.1k63562
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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