Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How big are JavaScript function objects?

I was just wondering how the overhead is on a function object.

In an OOP design model, you can spawn up a lot of objects each with their own private functions, but in the case where you have 10,000+, these private function objects, I assume, can make for a lot of overhead.

I'm wondering if there are cases where it would be advantageous enough to move these functions to a utility class or external manager to save the memory taken up by these function objects.

like image 927
Kevin Wang Avatar asked Jun 25 '13 22:06

Kevin Wang


People also ask

What is a function in JavaScript?

We're building the largest self-service freelancing marketplace for people like you. In JavaScript, functions are called Function Objects because they are objects. Just like objects, functions have properties and methods, they can be stored in a variable or an array, and be passed as arguments to other functions.

How to get the size of an object in JavaScript?

So, here is a hacky method that might help you. JavaScript doesn't provide a "sizeof" method for a reason: each JavaScript implementaion is different. In Google Chrome for example the same page uses about 66 bytes for each object (judging from the task manager at least).

How much memory does a JavaScript object use?

It provides some theoretical number akin to C sizeof semantics, but not the actual amount of memory used. Objects themselves consume space (so sizeof (new Date ()) should be >0), and JS engines save memory by deduping strings and storing them in single-byte encodings when possible, for example.

What is an object in JavaScript?

JavaScript objects are containers for named values called properties or methods. Spaces and line breaks are not important. An object definition can span multiple lines: The name:values pairs in JavaScript objects are called properties: Objects can also have methods.


2 Answers

This is how Chrome handles functions, and other engines may do different things.

Let's look at this code:

var funcs = [];
for (var i = 0; i < 1000; i++) {
    funcs.push(function f() {
        return 1;
    });
}
for (var i = 0; i < 1000; i++) {
    funcs[0]();
}

http://jsfiddle.net/7LS6B/4/

Now, the engine creates 1000 functions.

The individual function itself takes up almost no memory at all (36 bytes in this case), since it merely holds a pointer to a so-called SharedFunctionInfo object, which is basically a reference to the function definition in your source code*. This is called lazy parsing.

Only when you run it frequently does the JIT kick in, and creates a compiled version of the function, which requires more memory. So, funcs[0] takes up 256 bytes in the end:

Heap analyzer screenshot

*) This is not exactly true. It also holds scope information and the function's name and other metadata, which is why it has a size of 592 bytes in this case.

like image 154
user123444555621 Avatar answered Sep 20 '22 18:09

user123444555621


First of all, it's common to place methods in the object constructor prototype, so they'll be shared among all instances of a given object:

function MyObject() {
    ....
}

MyObject.prototype.do_this = function() {
   ...
}

MyObject.prototype.do_that = function() {
   ...
}

Also note that a "function object" is a constant code-only block or a closure; in both cases the size is not related to the code:

x = [];
for (var i=0; i<1000; i++) {
    x.push(function(){ ... });
}

The size of each element of the array is not going to depend on the code size, because the code itself will be shared between all of the function object instances. Some memory will be required for each of the 1000 instances, but it would be roughly the same amount required by other objects like strings or arrays and not related to how much code is present inside the function.

Things would be different if you create functions using JavaScript's eval: In that case I'd expect each function to take quite a bit and proportional to code size unless some super-smart caching and sharing is done also at this level.

like image 34
6502 Avatar answered Sep 19 '22 18:09

6502