Javascript 内存中函数的大小

Javascript 内存中函数的大小,javascript,function,memory,Javascript,Function,Memory,我一直在使用sizeof.js探索javascript中各种对象的大小。当我检查函数的大小时,无论函数包含多少行代码,我都会得到零字节。例如: alert(sizeof( function(){ return 1; } )); 这将返回零的大小。即使我给这个函数很多行代码,我也得到了零字节的大小。存储字符串所需的内存量取决于字符串的长度。但函数的复杂性或大小似乎无关紧要。为什么会这样?如果您查看sizeof库的源代码,您会发现sizeof库中没有处理对象类型函数,因此默认情况下

我一直在使用
sizeof.js
探索javascript中各种对象的大小。当我检查函数的大小时,无论函数包含多少行代码,我都会得到零字节。例如:

alert(sizeof(
  function(){
    return 1;
  }
));

这将返回零的大小。即使我给这个函数很多行代码,我也得到了零字节的大小。存储字符串所需的内存量取决于字符串的长度。但函数的复杂性或大小似乎无关紧要。为什么会这样?

如果您查看sizeof库的源代码,您会发现sizeof库中没有处理对象类型
函数
,因此默认情况下会返回大小
0

/*

sizeof.js

A function to calculate the approximate memory usage of objects

Created by Stephen Morley - http://code.stephenmorley.org/ - and released under
the terms of the CC0 1.0 Universal legal code:

http://creativecommons.org/publicdomain/zero/1.0/legalcode

*/

/* Returns the approximate memory usage, in bytes, of the specified object. The
 * parameter is:
 *
 * object - the object whose size should be determined
 */
function sizeof(object){

  // initialise the list of objects and size
  var objects = [object];
  var size    = 0;

  // loop over the objects
  for (var index = 0; index < objects.length; index ++){

    // determine the type of the object
    switch (typeof objects[index]){

      // the object is a boolean
      case 'boolean': size += 4; break;

      // the object is a number
      case 'number': size += 8; break;

      // the object is a string
      case 'string': size += 2 * objects[index].length; break;

      // the object is a generic object
      case 'object':

        // if the object is not an array, add the sizes of the keys
        if (Object.prototype.toString.call(objects[index]) != '[object Array]'){
          for (var key in objects[index]) size += 2 * key.length;
        }

        // loop over the keys
        for (var key in objects[index]){

          // determine whether the value has already been processed
          var processed = false;
          for (var search = 0; search < objects.length; search ++){
            if (objects[search] === objects[index][key]){
              processed = true;
              break;
            }
          }

          // queue the value to be processed if appropriate
          if (!processed) objects.push(objects[index][key]);

        }

    }

  }

  // return the calculated size
  return size;

}
/*
sizeof.js
用于计算对象的近似内存使用量的函数
斯蒂芬·莫利创作-http://code.stephenmorley.org/ -并根据
《CC0 1.0通用法典》的条款:
http://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
/*返回指定对象的大致内存使用量(字节)。这个
*参数为:
*
*对象-应确定其大小的对象
*/
函数sizeof(对象){
//初始化对象列表和大小
var objects=[object];
变量大小=0;
//在对象上循环
对于(var index=0;index
您可以看到,
size
是用值
0
初始化的,并且类型
函数
没有在
开关
语句中处理,因此对于函数它总是返回
0

<“虽然JavaScript不包含查找对象的确切内存使用情况的机制,但sizeof.js提供了一个函数,用于确定所使用的大致内存量。”(我的重点。)


结果是“近似的”“因为它受到浏览器提供的信息的限制,而且浏览器显然不提供功能的大小信息。这并不奇怪,因为函数的代码——浏览器执行的实际字节码或机器码——是从程序中看不到的实现细节。函数基本上是一个黑匣子。

sizeof.js所做的只是总结对象的iterable属性的大小或纯量值的大小。它使用固定的已知大小的标量值。所以很明显,结果非常不准确

它无法计算函数的大小,因为:

  • 它依赖于实现
  • 它甚至可能在运行时发生变化
  • ECMAScript不声明函数必须如何实现

  • 很难确定一个函数有多少内存

    该函数中的所有局部变量都需要考虑,甚至clousure也需要考虑在内


    正如@Arun所指出的,在sizeof.js中函数是不被考虑的。那么,在运行时存储函数所需的内存是否确实取决于函数的代码行长度?@gloo:这取决于很多事情,如果VM optimizer决定,在运行时可能会发生变化。@gloo我认为javascript解释器会做一些更改(优化),因此它将取决于很多因素。。。此外,从我所读到的内容来看,优化级别可能会根据函数的使用频率而变化。因此,假设较长的函数在运行时需要更多内存来存储函数本身是否合理?@gloo:不,您最好不要做出这样的假设“做得更多”的函数可能具有更大的代码大小,但它与函数源代码的长度没有直接关系。