Removing items from array (v11)

Revision 11 of this benchmark created by boris on


Preparation HTML

<script>
// Array Remove - By John Resig (MIT Licensed)
// simplified
Array.prototype.remove = function(ind) {
  var rest = this.slice(ind + 1);
  this.length = ind;
  return this.push.apply(this, rest);
};
</script>

Setup

var a = [], i = 0;
    while (i < 1000)
        a[i++] = Math.floor(Math.random() * 10);

Test runner

Ready to run.

Testing in
TestOps/sec
Array.splice
var b = a.slice(), i = 0, el;
while (i < b.length) {
    el = b[i++];
    if (el === 5)
        b.splice(i-1, 1);
}
ready
Parsing array
var b = a.slice(), el,
i = 0, j = 0;
while (i < b.length) {
    el = b[i++];
    if (b !== 5)
        b[j++] = b[i-1];
}
b.length = j;
ready
Creating a new array
var b = a.slice();
var c = [], i = 0, el;
while (i < b.length) {
    el = b[i++];
    if (el !== 5)
        c[c.length] = el;
}
ready
Array.remove (John Resig)
var b = a.slice(), i = 0, el;
while (i < b.length) {
    el = b[i++];
    if (el === 5)
        b.remove(i-1);
}
ready

Revisions

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