Get last item from array (v17)

Revision 17 of this benchmark created on


Preparation HTML

<script>
Object.defineProperty(Array.prototype, 'last', {configurable:true, enumerable: false,
  get:function() {return this[this.length - 1];},
  set:function(value) {if(this.length > 0) this[this.length - 1] = value; else this.push(value)}
});
var array = [1,2,3,5,4,6,85,2,5,7,4,4,4,234,23];
var value;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
array[array.length - 1]
value = array[array.length - 1];
ready
array.slice(-1)[0];
value = array.slice(-1)[0];
ready
array.slice().pop();
value = array.slice().pop();
ready
[].concat(array).pop();
value = [].concat(array).pop();
ready
array.push(item = array.pop())
var item;
value = array.push(item = array.pop());
ready
array.reverse()[0];
value = array.reverse()[0];
ready
array last getter
value = array.last;
ready

Revisions

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