long array data access

JavaScript performance comparison

Test case created by Paweł Sierszeń

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var results_short = [
        'NORTH',
        'NORTHEAST', 'NORTHEAST',
        'EAST', 'EAST',
        'SOUTHEAST', 'SOUTHEAST',
        'SOUTH', 'SOUTH',
        'SOUTHWEST', 'SOUTHWEST',
        'WEST', 'WEST',
        'NORTHWEST', 'NORTHWEST',
        'NORTH', 'NORTH'];
     
    function test_short(angle) {
        return results_short[Math.floor(angle/22.5)];
    }
   
    var results_long = [];
   
    for (var i = 0; i < 360; i++) {
        results_long[i] = results_short[Math.floor(i/22.5)];
    }
   
    function test_long(angle) {
        return results_long[angle];
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Short
var i;
for (i = 0; i < 360; i++) {
    test_short(i);
}
pending…
Long
var i;
for (i = 0; i < 360; i++) {
    test_long(i);
}
pending…
Short (no function call)
var i;
for (i = 0; i < 360; i++) {
    results_short[Math.floor(i/22.5)];
}
pending…
Long (no function call)
var i;
for (i = 0; i < 360; i++) {
    results_long[i]
}
pending…
Long (no function call) with floor
var i;
for (i = 0; i < 360; i++) {
    results_long[Math.floor(i)]
}
pending…
experimental
var i;
for (i = 0; i < 360; i++) {
    results_long[Math.floor(i/22.5)]
}
pending…
experimental - end of array
var i;
for (i = 0; i < 360; i++) {
    results_long[(i >> 1) + 180]
}
pending…
experimental - beginning of array
var i;
for (i = 0; i < 360; i++) {
    results_long[(i >> 1)]
}
pending…
experimental - every second element
var i;
for (i = 0; i < 360; i++) {
    results_long[(i << 1) % 360]
}
pending…
experimental - random element
var i;
for (i = 0; i < 360; i++) {
    results_long[Math.floor(Math.random()*360)]
}
pending…

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

Compare results of other browsers

0 comments

Add a comment