Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome thinks 99,999 is drastically different than 100,000

I just ran into a very interesting issue when someone posted a jsperf benchmark that conflicted with a previous, nearly identical, benchmark I ran.

Chrome does something drastically different between these two lines:

new Array(99999);  // jsperf ~50,000 ops/sec new Array(100000); // jsperf ~1,700,000 ops/sec 

benchmarks: http://jsperf.com/newarrayassign/2

I was wondering if anyone has any clue as to what's going on here!

(To clarify, I'm looking for some low-level details on the V8 internals, such as it's using a different data structure with one vs the other and what those structures are)

like image 265
Nobody Avatar asked Jul 15 '11 18:07

Nobody


1 Answers

Just because this sounded pretty interesting, I searched through the V8 codebase for a static defined as 100000, and I found this kInitialMaxFastElementArray var, which is the subsequently used in the builtin ArrayConstructInitializeElements function function. While I'm not a c programmer and don't know the nitty-gritty here, you can see that it's using an if loop to determine if it's smaller than 100,000, and returning at different points based on that.

like image 118
eykanal Avatar answered Oct 16 '22 02:10

eykanal