Shift vs manually shifting entire array

JavaScript performance comparison

Revision 3 of this test case created by vincent piel

Info

from arr = [0,1,2,3,4,5,6,7,8,9];

to arr = [1,2,3,4,5,6,7,8,9];

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [];
    for (var idx = 0; idx < 10000; idx++) {
      arr[idx] = idx;
    }
};
</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
Manually
var res = arr[0];
for (var i = 0; i < arr.length - 1; i++) {
  arr[i] = arr[i - 1];
}
pending…
Shift
var res = arr.shift();
pending…
Manually /wise
var res = arr[0];
var i= 0, len = arr.length;
if (!len) { return }
while(i<len) { arr[i] = arr[i+1]; i++; }
arr.length--;
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment