Skip to content

Instantly share code, notes, and snippets.

@delasy
Created April 24, 2021 23:49
Show Gist options
  • Save delasy/71a05ee307d85190efdeb832984cc927 to your computer and use it in GitHub Desktop.
Save delasy/71a05ee307d85190efdeb832984cc927 to your computer and use it in GitHub Desktop.
Program to check speed of different setTimeout arguments
This program was created to determine which setTimeout is faster
Timeout of -1 = 1ms
Timeout of 0 = 1ms
Timeout of 1 = 1ms
Timeout of 2 = 2ms
Timeout of 3 = 3ms
Timeout of 4 = 5ms
Timeout of 5 = 6ms
Timeout of 6 = 7ms
Timeout of 7 = 8ms
Timeout of 8 = 9ms
Timeout of 9 = 10ms
Timeout of 10 = 12ms
Timeout of 11 = 13ms
Timeout of 12 = 14ms
Timeout of 13 = 15ms
Timeout of 14 = 16ms
Timeout of 15 = 17ms
Timeout of 16 = 19ms
Timeout of 17 = 20ms
Timeout of 18 = 21ms
Timeout of 19 = 23ms
Timeout of 20 = 23ms
Timeout of 21 = 25ms
Timeout of 22 = 26ms
Timeout of 23 = 26ms
Timeout of 24 = 27ms
Timeout of 25 = 28ms
Timeout of 26 = 30ms
Timeout of 27 = 32ms
Timeout of 28 = 30ms
Timeout of 29 = 33ms
Timeout of 30 = 34ms
Timeout of 31 = 35ms
Timeout of 32 = 35ms
Timeout of 33 = 36ms
Timeout of 34 = 38ms
Timeout of 35 = 39ms
Timeout of 36 = 39ms
Timeout of 37 = 38ms
Timeout of 38 = 40ms
Timeout of 39 = 43ms
Timeout of 40 = 41ms
Timeout of 41 = 41ms
Timeout of 42 = 42ms
console.log('This program was created to determine which setTimeout is faster')
const TEST_DEPTH = 42
const TEST_REPEAT = 8
function test_body (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
async function test (ms) {
let prev = Date.now()
let avg = 0
for (let i = 0; i < TEST_REPEAT; i++) {
await test_body(ms)
const curr = Date.now()
avg += curr - prev
prev = curr
}
return Math.floor(avg / TEST_REPEAT)
}
async function main () {
for (let i = -1; i <= TEST_DEPTH; i++) {
const result = await test(i)
console.log(`Timeout of ${i} = ${result}ms`)
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment