Remove element from Array - slice, splice, delete

JavaScript performance comparison

Test case created by

Preparation code

<script>

var removeAtIndexSlice = function (arr, idx) {
	arr = arr.slice(0, idx).concat(arr.slice(idx + 1));
};

var removeAtIndexSplice = function (arr, idx) {
	arr.splice(idx, 1);
};

var removeAtIndexDeleteMap = function (arr, idx) {
	delete arr[idx];
	arr = arr.filter(function (x) { return typeof x !== "undefined"; });
};

</script>
      
<script>
Benchmark.prototype.setup = function() {
  var array = [];
  for (var i=0; i<10000; ++i) {
    array[i] = i;     
  };

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Slice
removeAtIndexSlice(array, 5000);
pending…
Splice
removeAtIndexSplice(array, 5000);
pending…
Delete
removeAtIndexDeleteMap(array, 5000);
pending…

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

0 Comments