Last Array Element

JavaScript performance comparison

Revision 6 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);
  }
  
  if (typeof Array.prototype.peek === "undefined") {
  	Object.defineProperty(Array.prototype, "peek", {
  		value: function (value) {
  			return this[this.length - 1];
  		},
  		enumerable: false,
  		writable: false,
  		configurable: false
  	});
  }
  

};
</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
Peek
var last = arr.peek();
pending…
Control (elems pre-cached)
var last = arr[elems-1];
pending…
Push-Pop
var last;
arr.push(last = arr.pop());
pending…
.length-1
var last = arr[arr.length - 1];
pending…
slice(-1)[0]
var last = arr.slice(-1)[0];
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