Using length magic function is bad

JavaScript performance comparison

Test case created by Dan Andreescu

Preparation code

Benchmark.prototype.setup = function() {
    var a = [1,2,3,4,5,6,7,8,9,0,123,12,1,41,24,124,124,124,1245,13,52,35,233,5,2345,34,63,46,36,34,63,46,346,3,763,7,3457,35,234,35,57,35,3346,36,3,63,4,4,6,8,4,8456,84,5,3,612,36,347,3,734,57,457,45,735,74,57,47,3,46];
    var len = a.length;

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
using length in the for loop
for (i = 0; i < a.length; i++) {
    var k = a[i];
caching length before the loop
for (i = 0; i < len; i++) {
    var k = a[i];

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers


Comment form temporarily disabled.

Add a comment