Created
January 28, 2021 07:31
-
-
Save vitqst/9577b158741cf6b51e9e25fdcc5e025e 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
function buildArray(n) { | |
const arr = []; | |
for (let i = 0; i < n; i++) { | |
arr.push(i); | |
} | |
} | |
function buildArray2(n) { | |
const arr = new Array(n).fill(0); | |
for (let i = 0; i < n; i++) { | |
arr[i] = (i); | |
} | |
} | |
function measureTime(callback, ...a) { | |
const start = new Date().getTime(); | |
buildArray(...a) | |
const end = new Date().getTime(), | |
time = end - start, | |
execs = 1000 / time; | |
console.log(`\nFUNCTION: ${callback.name}`) | |
console.log(`time in ms: ${time}`); | |
console.log(`operations per second: ${execs}`); | |
} | |
measureTime(buildArray, 20000000) | |
measureTime(buildArray2, 20000000) | |
// FUNCTION: buildArray | |
// time in ms: 318 | |
// operations per second: 3.1446540880503147 | |
// | |
// FUNCTION: buildArray2 | |
// time in ms: 262 | |
// operations per second: 3.816793893129771 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment