@ronb yes, internally there is "high bits" and "low bits", that doesn't change based on value
as far as I remember, at least v8 can detect 31-bit ints and it’s more efficient to store them https://www.html5rocks.com/en/tutorials/speed/v8/#toc-topic-numbers
just compared memory consumption of my double trick to goog.math.Long. Using a double in this way is much smaller on every browser: Firefox 4x, Chrome 5x, Safari 5x
@tonsky @thedavidmeister the 31 bit rule only applies to 32 bit machines. 64bit engines use NAN-boxing. I found a good description here: https://softwareengineering.stackexchange.com/questions/185406/what-is-the-purpose-of-nan-boxing
a more in-depth analysis: http://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations
basically a double is stored in the pointer directly with one of the redundant NaN values active (JS only has a single NaN representation)
how do you measure memory consumption @ronb?
also, using Long means the values will be boxed, no wonder memory consumption jumped up
> V8 doesn't actually use 63-bit smi values on 64-bit machines, AFAIK. so just 31 bits for fast immediate integers :(
@tonsky I used memory snapshots>containment in chrome to check memory consumption
> if a numeric value is bigger than 31 bits, V8 will box the number, turning it into a double and creating a new object to put the number inside.
could’nt figure out how to do this in Safari, so i just used all js memory and compared
https://thibaultlaurens.github.io/javascript/2013/04/29/how-the-v8-engine-works/
hmm strange, that would mean my measurements were incorrect somehow. thanks for the info
it seems like v8 and webkit use different approaches
(nan-boxing vs tagging)
> NaN-boxing has the obvious advantage of not allocating doubles on the heap. This reduces cache pressure, GC pressure, and such. That's why Moz and JSC chose it. V8 on the other hand has not chosen it, at least not yet, anyway.
damn, it’s so tricky to convince JS to exactly what you want
Haha yeah there is soo much magic going on 😄