Skip to content

Instantly share code, notes, and snippets.

@kevduc
Last active May 25, 2021 12:17
Show Gist options
  • Save kevduc/74856214b493663242ba581c234bc4ae to your computer and use it in GitHub Desktop.
Save kevduc/74856214b493663242ba581c234bc4ae to your computer and use it in GitHub Desktop.
const measure = (f, sampleSize, ...args) => {
const start = Date.now()
for (let i = 0; i < sampleSize; i++) f(...args)
return (Date.now() - start) / sampleSize
}
const perfDiff = (value1, value2) => (100 * (value1 - value2)/value1).toFixed(2) + '%'
const callback = (_,i) => i
const createArrayFillMapMethod = (size) => Array(size).fill().map(callback)
const createNewArrayFillMapMethod = (size) => new Array(size).fill().map(callback)
const createArrayFromMethod = (size) => Array.from({length: size}, callback)
const results = []
const sampleSize = 1e5
for (let size = 1; size <= 1e4; size *= 10) {
console.log(`Size ${size}...`)
const arrayFillMapMethodMeasure = 1000 * measure(createArrayFillMapMethod, sampleSize, size) // in µs
const newArrayFillMapMethodMeasure = 1000 * measure(createNewArrayFillMapMethod, sampleSize, size) // in µs
const arrayFromMethodMeasure = 1000 * measure(createArrayFromMethod, sampleSize, size) // in µs
// We use createArrayFillMapMethod as a reference to measure performance improvement
// - positive perfDiff => performance improvement
// - negative perfDiff => performance decrease
const perfDiff1 = perfDiff(arrayFillMapMethodMeasure, newArrayFillMapMethodMeasure)
const perfDiff2 = perfDiff(arrayFillMapMethodMeasure, arrayFromMethodMeasure)
results.push({size, arrayFillMapMethodMeasure, newArrayFillMapMethodMeasure, perfDiff1, arrayFromMethodMeasure, perfDiff2})
console.table(results)
}
@kevduc
Copy link
Author

kevduc commented May 23, 2021

On Windows 10 with a CPU Intel(R) Celeron(R) N4000 @ 1.10GHz using node v15.10.0

┌─────────┬────────┬───────────────────────────┬──────────────────────────────┬───────────┬────────────────────────┬────────────┐
 (index)   size   arrayFillMapMethodMeasure  newArrayFillMapMethodMeasure  perfDiff1  arrayFromMethodMeasure  perfDiff2  
├─────────┼────────┼───────────────────────────┼──────────────────────────────┼───────────┼────────────────────────┼────────────┤
    0          1            0.41                         0.25              '39.02%'            0.34             '17.07%' 
    1         10            0.48                         0.31              '35.42%'  │          1.45          │ '-202.08%' │
│    2    │    100 │           1.97            │             1.88             │  '4.57%'  │         13.33          │ '-576.65%' │
│    3    │   1000 │          16.8             │            15.59             │  '7.20%'  │        136.99          │ '-715.42%' │
│    4    │  10000 │         156.32            │           146.41             │  '6.34%'  │       1639.99          │ '-949.12%' │
└─────────┴────────┴───────────────────────────┴──────────────────────────────┴───────────┴────────────────────────┴────────────┘

All durations are average execution times in µs

  • Array.from(...) is 3 to more than 10 times slower to execute than the other 2 methods

  • Looks like new Array() is faster than Array() (need more data points)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment