Skip to content

Instantly share code, notes, and snippets.

@bayleedev
Last active June 15, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayleedev/0c1b5e77cee7c94e88a2eb7ef3d1e6c4 to your computer and use it in GitHub Desktop.
Save bayleedev/0c1b5e77cee7c94e88a2eb7ef3d1e6c4 to your computer and use it in GitHub Desktop.

Performance Test

The goal is to determine if the overhead of multiple setTimeout is heavier than a single setInterval.

Each test has 60 iterations of a function wait that takes 30ms to run, making the program time 60*30=1800ms, so running these files should capture the overhead by subtracting 1800ms from the total run time.

Timeout

It took around 1816ms or around ~16ms of overhead.

Interval

It took around 3779ms or aorund ~1979ms of overhead.

Timeout Recursive

It took around 3769ms or around ~1969ms of overhead.

function wait (delay) {
++waitCalls
if (typeof delay !== 'number') delay = 3
var start = new Date().getTime()
while (new Date().getTime() < start + delay) continue
}
function run () {
var id = setInterval(function () {
if (waitCalls >= 60) {
return clearInterval(id)
}
wait(30)
}, 0)
}
var start = Date.now()
var waitCalls = 0
run()
process.on('exit', function () {
console.log('count', waitCalls, Date.now() - start)
})
function wait (delay) {
++waitCalls
if (typeof delay !== 'number') delay = 3
var start = new Date().getTime()
while (new Date().getTime() < start + delay) continue
}
function run () {
setTimeout(function foo () {
wait(30)
if (waitCalls < 60) {
setTimeout(foo, 0)
}
}, 0)
}
var start = Date.now()
var waitCalls = 0
run()
process.on('exit', function () {
console.log('count', waitCalls, Date.now() - start)
})
function wait (delay) {
++waitCalls
if (typeof delay !== 'number') delay = 3
var start = new Date().getTime()
while (new Date().getTime() < start + delay) continue
}
function run () {
for (var i = 0; i < 60; i++) {
setTimeout(function () {
wait(30)
}, 0)
}
}
var start = Date.now()
var waitCalls = 0
run()
process.on('exit', function () {
console.log('count', waitCalls, Date.now() - start)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment