Skip to content Skip to sidebar Skip to footer

What Is The Size (in Memory) Of A Number?

What is the size of a number in JavaScript? For example, I know a single char in C is 1 byte. The size of an int is sizeof(int). The size on an int64_t is 64 bits, and so on. What

Solution 1:

You can't determine memory size of number value size in JS. It is engine-specific and can be different between different values in same engine. ECMAScript standard (e.g. ECMA-262) only defines observable behavior of numbers, but as long as behavior matches specification in the end, different JS VMs use all kinds of different number types under the hood for optimization purposes.

Standard sets no limits on what engines can use and defines no method to retrieve those implementation details. Nor any other part of spec relies on anything except observable behavior again. You can check out engine-specific details in its documentation or try engine-specific internals debugging tools, but you can't get this size data from JS code itself.

Solution 2:

Aside from what's mentioned in other answers, the reality is that modern engines use various optimizations, including storing numbers in various different methods (types...) depending on usage. This is one of the main ideas behind things like asm.js, and just to provide a simple example:

var i = 0;
while(i < 5) {
  console.log('hello');
  i++;
}

The engine can infer that i is an integer and optimize it's usage.

Post a Comment for "What Is The Size (in Memory) Of A Number?"