gloo
gloo

Reputation: 2580

size of a function in memory

I have been exploring the size of various objects in javascript using sizeof.js. When I examine the size of a function I get zero bytes no matter how many lines of code the function contains. For example:

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

This returns a size of zero. Even if I give the function many lines of code I get a size of zero bytes. The amount of memory needed to store a string is dependent on the length of the string. But the complexity or size of a function seems to be irrelevant. Why is this so?

Upvotes: 3

Views: 1891

Answers (4)

Sean
Sean

Reputation: 3042

it is too hard to determine how much memory a function is holding

all the local variable in that function need to be take into consideration, even clousure need to be take into count

as @Arun point out, function is not take into count in sizeof.js

Upvotes: 0

zerkms
zerkms

Reputation: 254926

What sizeof.js does is just summarizes sizes of iterable attributes of an objects or plain sizes of scalar values. It uses the fixed known sizes of scalar values for that. So obviously the result is pretty much inaccurate.

It cannot calculate the size of a function because:

  1. It is implementation dependent
  2. It may even vary in runtime
  3. ECMAScript does not declare how functions must be implemented

Upvotes: 2

Wyzard
Wyzard

Reputation: 34563

The website for sizeof.js says: "While JavaScript does not include a mechanism to find the exact memory usage of an object, sizeof.js provides a function that determines the approximate amount of memory used." (Emphasis mine.)

The result is "approximate" because it's limited by the information that the browser makes available, and the browser apparently doesn't provide size information for functions. This is unsurprising, since the code of a function — the actual bytecode or machine code that the browser executes — is an implementation detail that's not visible from within the program. A function is basically a black box.

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

If you look at the source code of the sizeof library, you can find that the object type function is not handled in the sizeof library, so by default the size 0 is returned.

/*

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;

}

You can see that size is initialized with value 0 and the type function is not handled in the switch statement so it will always return 0 for a function.

Upvotes: 2

Related Questions