Object delete vs. array splice vs. array delete

JavaScript performance comparison

Revision 2 of this test case created

Info

Is it faster to remove an item from an array (and thus allow the indices to be renumbered) or delete a property from an object. Also, checking deletion from array, but this does not renumber the indices so may not be preferable.

Preparation code

<script>
  var someArr, someObj;
</script>
<script>
Benchmark.prototype.setup = function() {
    someArr = [4, 5, 6];
    someObj = {a: 4, b: 5, c: 6};
};
</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
Splice
someArr.splice(1, 1);
pending…
Object Delete
delete someObj.b;
pending…
Array Delete
delete someArr[1];
pending…
Object sub-script Delete
delete someObj['b'];
pending…
Array indexOf splice
someArr.splice(someArr.indexOf(5), 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