Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Javascript Numbers get represented as 64bit numbers in a 32bit browser?

I'm a bit confused about the size of a javascript number in a 32bit browser. Is it still represented as a 64bit number with max value at 2^53?

like image 313
Nick Avatar asked Dec 12 '22 09:12

Nick


2 Answers

Answers couldn't be more wrong, it does depend on engine.

In V8 (Google Chrome, Opera, Node.js) 32-bit:

Integers that fit 31-bit signed representation (from -1073741824 to 1073741823) are represented directly by embedding it them in pointers.

Any other number is generally represented as a heap object that has a 64-bit double as a field for the numeric value (think of Java Double wrapper). In optimized functions such numbers can be temporarily stored directly on the stack and registers. Also certain kind of arrays can store doubles directly "permanently".

In V8 64-bit:

Same as 32-bit except integers can now fit in 32-bit signed representation (from -2147483648 to 2147483647) instead of 31-bit.

like image 68
Esailija Avatar answered Mar 09 '23 01:03

Esailija


Yes. A number in Javascript is a double precision floating point number. It's the same regardless of the platform that it runs on.

like image 21
Guffa Avatar answered Mar 08 '23 23:03

Guffa