JavaScript function's memory size
This seems rather obvious to me, but I just wanted to make sure. In terms of memory used when the foo
function is stored, would it better to do this:
function foo(){
var hey = {};
hey.a = 1;
hey.b = 1;
alert('done');
}
or
function foo(){
var hey = getHey();
alert('done');
}
function getHe开发者_运维问答y(){
var hey = {};
hey.a = 1;
hey.b = 1;
return hey;
}
Since getHey()
is just going to be a reference to the actual function, I'm not sure whether it stores foo
as is, or with an embedded getHey
.
logically the function will be stored in memory while the parent scope lives. and that reference does take a bit of memory but the real danger is usually the overhead when calling function in a loop, just having a single function call inside a large for-loop could add a lot of processing time.
They're separate functions. What'd probably save some space however is:
function foo() {
var hey = { a: 1, b: 1 };
}
精彩评论