Last Array Element

JavaScript performance comparison

Revision 9 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = [];
  var elems = Math.floor(Math.random() * 70 + 5);
  for (var i = 0; i < elems; i++) {
    arr.push(i);
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
.reverse()[0]
var last = arr.reverse()[0];
pending…
Control (elems pre-cached)
var last = arr[elems-1];
pending…
.length-1
var last = arr[arr.length - 1];
pending…
slice(-1)[0]
var last = arr.slice(-1)[0];
pending…
Push-Pop
var last;
arr.push(last = arr.pop());
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.

0 Comments