4 ms·
http://axod.net/arraytest.html http://axod.net/arraytest.html After 20 iterations: Browser Pre-alloc No pre-alloc Firefox 3.6.13 OSX
by axod 16y ago
http://axod.net/arraytest.html http://axod.net/arraytest.html
After 20 iterations:
Browser Pre-alloc No pre-alloc
Firefox 3.6.13 OSX 824ms 829ms
Safari 5.0.3 OSX 812ms 948ms
Chrome 9.0.597.16 OSX 1317ms 992ms
I'm pretty sure that in modern browsers new Array(length) doesn't allocate anything, it just sets the length property. The results I'm seeing would agree with Google really.
Perhaps you were seeing GC events slowing down the test?
The other thing about for(var i=0;i<arr.length;i++) is it can end up as an infinite loop if you're modifying the length inside the for loop:
for (var i=0;i<arr.length;i++) {
arr[10 + i*2] = "foo";
}
// Infinite loop.
- gruseom 16y agoPerhaps you were seeing GC events slowing down the test? Perhaps. Or perhaps it varies by array size?
- bluekeybox 16y agoThen do not write code like this.