3 ms·
The article says: Although it is necessary in Java, it is entirely pointless to specify the length of an array ahead of time in JavaScript. [...] Rather,
by gruseom 16y ago
The article says:
Although it is necessary in Java, it is entirely pointless to
specify the length of an array ahead of time in JavaScript. [...]
Rather, you can just set up an empty array and allow it to grow as
you fill it in. Not only is the code shorter, but it runs faster too.
Faster? That ought to raise suspicion. JS's dynamic hash-arrays are neat, but now they're supposed to be immune from the laws that govern memory allocation in any other language?
As it happens, I had occasion to test this a few months ago.
function preallocate(len) {
var arr = new Array(len);
for (var n = 0; n < len; n += 1) {
arr[n] = n;
};
return arr;
}
function noPreallocate(len) {
var arr = [];
for (var n = 0; n < len; n += 1) {
arr[n] = n;
};
return arr;
}
On my machine, noPreallocate is 4% faster in FF, but it's 15% slower in IE8 and a whopping 70% slower in Chrome.
- axod 16y agohttp://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.
- mnemonik 16y agoIt is times like this that jsperf.com comes in handy. We can see the aggregated results of everyone who runs the test, browser by browser. http://jsperf.com/allocating-array-length http://jsperf.com/allocating-array-length
- Seth_Kriticos 16y agoAgree, the author is detailing a lot of arcane specifics of JS that are probably valid in a very small subset of browsers, let's see what he writes: "Perhaps the most important thing these libraries do is make sophisticated vector graphics possible in Internet Explorer, where JavaScript performance is relatively poor." Well, yes. Sure, suit yourself, but I think that most modern browsers come with something called a JS code profiler/optimizer and JIT code (FF new JS engine and Chrome surely do, others probably too). Not to mention that Closure comes with it's own code optimizer that does this for the border cases. For sanities sake, please don't start to preach that we should do the work of a compiler and bend the code in that direction. Humans are bad at that, and it makes the code unmaintainable. There are some valid points in the article though, I admit, some pitfalls that might screw up when multiple JS frameworks are running (namespace issues) mostly.