Removing items from array

JavaScript performance comparison

Revision 11 of this test case created by boris

Preparation code

<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>
<script>
Benchmark.prototype.setup = function() {
    var a = [], i = 0;
    while (i < 1000)
        a[i++] = Math.floor(Math.random() * 10);
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/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);
}
pending…
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;
pending…
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;
}
pending…
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);
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment