开发者

JavaScript function's memory size

开发者 https://www.devze.com 2023-01-21 06:30 出处:网络
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:

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 };
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号