Pop vs Splice vs Length (v6)

Revision 6 of this benchmark created by David on


Description

Compares three ways of removing an item at specified index. The pop and length techniques reassign the last value to the index to remove before popping the last value. The splice just uses the function of the same name.

Setup

x = window.x = [];
    for (var i = 0; i < 10000; ++i) {
      x.push(i);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Pop
var n = 10;
var x = window.x;
for (;n-->0;) {
  x[x.indexOf(n*200)] = x[x.length-1];
  x.pop();
}
ready
Splice
var n = 10;
var x = window.x;
for (;n-->0;) {
  x.splice(x.indexOf(n*200), 1);
}
ready
Length
var n = 10;
var x = window.x;
var len = x.length-1;
for (;n-->0;) {
  x[x.indexOf(n*200)] = x[len--];
  if (len >= 0) x.length = len;
}
ready

Revisions

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