Last Array Element (v6)

Revision 6 of this benchmark created on


Setup

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
  	});
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Peek
var last = arr.peek();
ready
Control (elems pre-cached)
var last = arr[elems-1];
ready
Push-Pop
var last;
arr.push(last = arr.pop());
ready
.length-1
var last = arr[arr.length - 1];
ready
slice(-1)[0]
var last = arr.slice(-1)[0];
ready

Revisions

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