Object delete vs. array splice vs. array delete

JavaScript performance comparison

Revision 4 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 count = 100000;
  var deleteInc = 100;
</script>
<script>
Benchmark.prototype.setup = function() {
      var someArr = [];
      var someObj = {};
      for(var i = 0; i < count; i++)
      {
        someArr[i] = Math.floor(Math.random() * 1000000);
        someObj['a' + i] = Math.floor(Math.random() * 1000000);
      }
   
};
</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
for(var i = 0; i < count; i += deleteInc)
{
    someArr.splice(i, 1);
}
pending…
Object Delete
for(var i = 0; i < count; i += deleteInc)
{
    delete someObj['a' + i];
}
pending…
Array Delete
for(var i = 0; i < count; i += deleteInc)
{
    delete someArr[i];
}
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