Skip to content

Instantly share code, notes, and snippets.

@aapzu
Last active October 15, 2020 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aapzu/c83d3864df8be58aa8136a4bb53f9892 to your computer and use it in GitHub Desktop.
Save aapzu/c83d3864df8be58aa8136a4bb53f9892 to your computer and use it in GitHub Desktop.
const NUMBERS = 1000000
const TESTS = 100
const startTime = Date.now()
console.log(`Testing calculating the sum of ${NUMBERS} numbers\n`)
const forLoops = []
const whileLoops = []
const forOfLoops = []
const normalFunctionForEachs = []
const arrowFunctionForEachs = []
const normalFunctionReduces = []
const arrowFunctionReduces = []
const measure = (func, list) => {
const start = Date.now()
const val = func()
const end = Date.now()
list.push(end - start)
return val
}
function test() {
const numbers = Object.freeze([...Array(NUMBERS).keys()].map(() => parseFloat(Math.random() * NUMBERS)))
// Used to check that all the results are the same
const sums = []
// For loop
sums.push(measure(() => {
let sum = 0
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i]
}
return sum
}, forLoops))
// For-of loop
sums.push(measure(() => {
let sum = 0
for (let num of numbers) {
sum += num
}
return sum
}, forOfLoops))
// While loop
sums.push(measure(() => {
let sum = 0
let j = 0
while (j < numbers.length) {
sum += numbers[j]
j++
}
return sum
}, whileLoops))
// Regular function for-each
sums.push(measure(() => {
let sum = 0
numbers.forEach(function (num) {
sum += num
})
return sum
}, normalFunctionForEachs))
// Arrow function for-each
sums.push(measure(() => {
let sum = 0
numbers.forEach((num) => {
sum += num
})
return sum
}, arrowFunctionForEachs))
// Regular function reduce
sums.push(measure(() => {
return numbers.reduce(function (acc, it) {
return acc + it
}, 0)
}, normalFunctionReduces))
// Arrow function reduce
sums.push(measure(() => {
return numbers.reduce((acc, it) => acc + it, 0)
}, arrowFunctionReduces))
for (let i = 1; i < sums.length; i++) {
if (sums[i] !== sums[0]) {
throw new Error(`Something went wrong with the summing: ${JSON.stringify(sums)}`)
}
}
}
for (let i = 0; i < TESTS; i++) {
test()
}
const avg = (list) => list.reduce((acc, it) => acc + it / list.length, 0)
console.log(`Average of ${TESTS} tests:
forLoops: ${avg(forLoops).toFixed(3)}ms
whileLoops: ${avg(whileLoops).toFixed(3)}ms
forOfLoops: ${avg(forOfLoops).toFixed(3)}ms
normalFunctionForEachs: ${avg(normalFunctionForEachs).toFixed(3)}ms
arrowFunctionForEachs: ${avg(arrowFunctionForEachs).toFixed(3)}ms
normalFunctionReduces: ${avg(normalFunctionReduces).toFixed(3)}ms
arrowFunctionReduces: ${avg(arrowFunctionReduces).toFixed(3)}ms
Running all the tests took: ${((Date.now() - startTime) / 1000).toFixed(2)}s
`)
Testing calculating the sum of 500000 numbers
Average of 100 tests:
forLoops: 10.580ms
whileLoops: 7.100ms
forOfLoops: 20.460ms
normalFunctionForEachs: 33.670ms
arrowFunctionForEachs: 33.080ms
normalFunctionReduces: 9.340ms
arrowFunctionReduces: 9.310ms
Running all the tests took: 21.69s
Testing calculating the sum of 1000000 numbers
Average of 100 tests:
forLoops: 3.730ms
whileLoops: 2.060ms
forOfLoops: 6.860ms
normalFunctionForEachs: 4.230ms
arrowFunctionForEachs: 4.350ms
normalFunctionReduces: 2.410ms
arrowFunctionReduces: 2.570ms
Running all the tests took: 50.40s
Testing calculating the sum of 500000 numbers
Average of 100 tests:
forLoops: 1.950ms
whileLoops: 0.640ms
forOfLoops: 3.280ms
normalFunctionForEachs: 1.590ms
arrowFunctionForEachs: 1.410ms
normalFunctionReduces: 1.120ms
arrowFunctionReduces: 0.640ms
Running all the tests took: 21.13s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment