I have been using sizeof.js
to investigate the size of various objects in javascript. It appears from this that the size of a function is essentially zero bytes, regardless of how many instructions the function executes. Why is this so?
|
|||||||||
|
In most languages, code and data are different things. They occupy different parts of memory and don't interact with each other. When you ask the code "how big is this" you are asking about "how big is the instantiated object". When looking at this you through tools like sizeof.js, you are asking the question "how big is the object representing the function pointer". And in this case, the object doesn't have any additional properties its size is negligible. Asking how big something in a language like javascript which is often not compiled down too far becomes one of "how big is this parse tree", which doesn't have any real meaning at runtime... nor is it always accessible. Some languages such as Lisp, where the data and code can mix a bit more freely (the code is data), such a question can be slightly more meaningful, though not really. While I'm sure if I fought with it long enough (my lisp is a bit rusty and I never delved into that realm of lisp metaprogramming), I could come up with:
such that it would print out a value of 2, or 4 depending on how you wanted to define that. But it's not a meaningful number in most cases. Nor is the "how big is that function?" question asked of a javascript function pointer. It is. And whats more, under different javascript engines, if you could get a number, it would likely be different. So:
|
|||||||||
|
Look at the source. All it is doing is using a simple heuristic. It assumes bools are four bytes, numbers are eight bytes and strings are two bytes per character. Any or all of these could be wrong. (Especially the string size estimation as it's clearly assuming no interesting Unicode.) If it gets an object, it just treats its as a tree, adding up the sizes noted above non-object members and then adding the size of child objects in the same way. This is decent enough if you want an estimate. What it is not giving you is all the overhead used to store this stuff. You aren't seeing the space used by all these references to objects, for instance. In JavaScript, a function is just an object, and usually it is an object with no associated data members or child objects. So TL;DR - This is not like C |
|||||||||
|