immutable-splice

JavaScript performance comparison

Test case created by lucascaro

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let arr = Array(1e6).fill(0);

};
</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
const replace = arr => index => val => {
  arr.splice(index, 1, val);  //mutable and uncomfortable
  return arr;  //return the original/target itself...
};

replace(arr)(2)(999);
pending…
Slice
const replace = arr => index => val => (
  [...arr.slice(0, index), val, ...arr.slice(index + 1)]
);

replace(arr)(2)(999)
pending…
map
const replace = arr => index => val => {
  return arr.map((original, i) => i === index ? val : original);
};

replace(arr)(2)(999)
pending…
copy
const replace = arr => index => val => {
  const newArray = arr.concat();
  newArray[index] = val;
  return newArray;
};

replace(arr)(2)(999)
pending…
concat slices
const replace = arr => i => val =>
  i >= arr.length
    ? arr
    : arr.slice(0,i).concat(val, arr.slice(i+1))

replace(arr)(2)(999)
pending…

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

Compare results of other browsers

0 Comments