Apply vs spread

Benchmark created by adam-stover on


Setup

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)
  }

Teardown



            arr = []
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
spread
const result = arr.reduce((newArr, subArr) => {
  newArr.push(...subArr)
  return newArr
}, [])

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

const min = Math.min(...result)
const max = Math.max(...result)
ready
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)
ready
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)
ready

Revisions

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

  • Revision 1: published by adam-stover on