Array optimization

Benchmark created by Michael Geary on


Description

Compare array access speed for an optimizable array which contains only numbers vs. a non-optimizable array that has an object in the first element and numbers in the rest.

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var a = [ 0 ], b = [ { valueOf: function() { return 0; } } ];
    for( var i = 1;  i < 1000000;  ++i ) {
        a[i] = b[i] = i;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Fast array
var x = 0;
for( var i = 0;  i < 1000000;  ++i ) {
    x += a[i];
}
 
ready
Slow array
var x = 0;
for( var i = 0;  i < 1000000;  ++i ) {
    x += b[i];
}
 
ready

Revisions

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

  • Revision 1: published by Michael Geary on