Apply vs spread

JavaScript performance comparison

Test case created by adam-stover

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let arr = []
  for (let i = 0; i < 100; i++) {
    const subarr = []
    for (let i = 0; i < 100; i++) {
      subarr.push(Math.random() * i)
    }
    arr.push(subarr)
  }

};

Benchmark.prototype.teardown = function() {
  arr = []

};
</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
spread
const result = arr.reduce((newArr, subArr) => {
  newArr.push(...subArr)
  return newArr
}, [])

const min = Math.min(...result)
const max = Math.max(...result)
pending…
concat
const result = arr.reduce((newArr, subArr) => {
  return newArr.concat(subArr)
}, [])

const min = Math.min(...result)
const max = Math.max(...result)
pending…
apply
const result = arr.reduce((newArr, subArr) => {
  Array.prototype.push.apply(newArr, subArr)
  return newArr
}, [])

const min = Math.min.apply(null, result)
const max = Math.max.apply(null, result)
pending…
reflect apply
const result = arr.reduce((newArr, subArr) => {
  Reflect.apply(Array.prototype.push, newArr, subArr)
  return newArr
}, [])

const min = Reflect.apply(Math.min, null, result)
const max = Reflect.apply(Math.max, null, result)
pending…

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

0 Comments