Last active
October 15, 2020 18:05
-
-
Save aapzu/c83d3864df8be58aa8136a4bb53f9892 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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