Filter vs splice

JavaScript performance comparison

Test case created by Tom Dale

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let inputs = [];
  for (var i = 0; i < 10000; i++) {
    inputs.push({ index: 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
Splice (Mutable)
var indexToRemove = 5000;
inputs.splice(indexToRemove, 1);
var outputs = inputs
pending…
Filter (Immutable)
var indexToRemove = 5000;
var outputs = inputs.filter(function(el, idx) { return idx !== indexToRemove; });
pending…
Slice (Immutable)
var indexToRemove = 5000;
var outputs = inputs.slice(0, indexToRemove).concat(inputs.slice(indexToRemove+1));
pending…

Revisions

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

0 Comments