array shift vs looping

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var MAX_SIZE = 6;
    var a = [];
    var pos = 0;
};
</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
shift
var a = [];
for (var i = 0; i < 100; ++i) {
  a.push(1);
  while (a.length > MAX_SIZE) {
    a.shift();
  }
}
pending…
loop
var a = new Array(MAX_SIZE);
for (var i = 0; i < 100; ++i) {
  a[pos] = 1;
  pos = (pos + 1) % MAX_SIZE;
}
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