简体   繁体   English

JavaScript声明的函数在加载脚本时消耗多少内存?

[英]How much memory does a JavaScript declared function consume on script loading?

I have taken some JavaScript courses lately (at codeschool ) and, in one of them, they said: 我最近(在codeschool )上过一些JavaScript课程,其中之一是,他们说:

  • A function declaration (fd) builds in memory immediately when program loads; 程序加载时, 函数声明 (fd)立即建立在内存中;
  • A function expression (fe) builds in memory dynamically during execution. 函数表达式 (fe)在执行期间动态建立在内存中。

Thus fe has an advantage over fd which is saving some memory at loading time - I concluded. 因此,相对于fd,fe具有优势,后者在加载时节省了一些内存 -我得出结论。

So, for example, considering following two snipped codes below, there won't be any extra memory allocation on loading time because foo is going to be assigned to undefined thanks to hoisting. 因此,例如,考虑下面的两个片段代码,加载时将不会有任何额外的内存分配,因为由于提升,会将foo分配给undefined

// Original code
var foo = function (bar) {
  console.log(bar);
};

Below we have code after hoisting 下面是吊装后的代码

// Code after hoisting
var foo = undefined;

foo = function (bar) {
  console.log(bar);
};

In shortness, foo is undefined and the function expression will only be assigned to foo once code runs. 简而言之, fooundefined并且仅在代码运行后才将函数表达式分配给foo Thus it won't allocate extra memory on loading time. 因此,它不会在加载时分配额外的内存。

Ok. 好。 No problems till now. 到现在没有问题。 However, " if fe has an advantage over fd which is saving some memory at loading time " following code should take some extra memory to be allocated at loading time since it's a function declaration. 但是,“ 如果fe比fd有优势,那就是在加载时节省了一些内存 ”,下面的代码应该在加载时分配一些额外的内存,因为它是函数声明。

function foo(bar) {
  console.log(bar);
}

So the question is: is my reasoning right? 所以问题是:我的推理对吗? If so, how much memory does it take at loading time and how is it calculated? 如果是这样,加载时需要占用多少内存,以及如何计算?

Yes, you're right because all function declarations are hoisted to the top and (usually) compiled on the spot. 是的,您是对的,因为所有函数声明都悬挂在顶部,并且(通常)是在现场编译的。 Function expressions aren't generally compiled until run-time because they may or may not be used at all: 通常在运行时才编译函数表达式,因为它们可能会或根本不会使用:

var f;
if (condition) {
  f = function() { ... };
}
// f could be undefined

As for how much memory it takes, that depends on the engine implementation. 至于需要多少内存,取决于引擎的实现。 How they store: 他们如何存储:

  1. The executable code for the function 该函数的可执行代码
  2. Bindings for argument names 参数名称的绑定
  3. Closure values 闭包值
  4. String value of the code (if the engine supports it) 代码的字符串值(如果引擎支持)
  5. Any number of other values that the engine developers have decided they require to implement functions 引擎开发人员决定实现功能所需的任何其他值

all depends on the internal design of the engine. 一切都取决于发动机的内部设计。 You could perform some testing by performing a heap dump before and after functions are created but that actual number would vary as optimizations are added and dropped. 您可以通过在创建函数之前和之后执行堆转储来执行一些测试,但是实际数量会随着添加和删除优化而有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中重新创建函数需要多少内存? - How much memory does it take to re-create a function in JavaScript? ECMAScript / JavaScript字符串中的每个字符占用多少RAM? - How much RAM does each character in ECMAScript/JavaScript string consume? 如何引用将在脚本加载后声明的 function - How to refer to a function that will be declared after script loading 在Javascript(V8)中,为什么数组上的forEach比简单的for循环消耗更多的内存? - In Javascript (V8) why does forEach on an array consume much more memory than a simple for loop? 我能知道JavaScript中的函数调用使用了多少内存吗? - Can I know how much memory is used by a function call in JavaScript? 找出脚本使用了多少内存 - Find out how much memory is used by the script 对于Javascript中的嵌套对象,{}消耗的内存少于[]吗? - Does {} consume less memory than [] for nested objects in Javascript? 在不使用赋值的情况下使用var声明JavaScript中的变量会消耗内存吗? - Does declaring a variable in JavaScript with var without assignment consume memory? 在循环中使用JavaScript regexp文字会消耗不必要的内存吗? - Does using JavaScript regexp literals in loops consume unnecessary memory? 空的JavaScript对象有多少内存? - How much memory is an empty JavaScript object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM