- laps refers to number of laps to run (positive integer)
- racers refers to function(s) to race (array or single function)
- Use JavaScript exponential notation like
1e3
(rather than1000
) for expressing large numbers - Accurate results require many laps. The ideal amount depends on how complex the racers are:
Infinity
speeds and0
times both indicate inadequate laps- Results taking too long indicates excessive laps. Use less laps or use the
.async
syntax
- Time how long it takes each racer to run the given number of laps
- @return array of times measured in milliseconds
lap.time(1e5, [
function() { document.getElementById('example') },
function() { document.querySelector('#example') },
function() { document.querySelectorAll('#example')[0] }
]) // => [40.000000000873115, 44.99999999825377, 116.00000000180444]
- Estimate each racer's average speed over a course of laps
- @return array of speeds measured in operations per second
lap.speed(1e5, [
function() { document.getElementById('example') },
function() { document.querySelector('#example') },
function() { document.querySelectorAll('#example')[0] }
]) // => [2500000.0004001777, 2222222.2219491494, 884955.7522315352]
- Get a hi-resolution timestamp
- @return number measured in milliseconds
lap.timestamp() // => 1610.000000000582
- Methods are callable
.async
with the same arguments plus a(err, result)
callback - @return undefined
lap.time.async(laps, racers, callback)
lap.speed.async(laps, racers, callback)
lap.timestamp.async(callback)
lap.time.async(1e5, function() {
[].concat([0, 1, 2, 3])
}, function(err, result) {
err || console.log(result[0] + ' ms')
}) // => undefined
.sync
methods are included for expressiveness but these are just aliases. lap.time === lap.time.sync
etc.
lap.time.sync(1e5, [
function() { document.getElementById('example') },
function() { document.querySelector('#example') },
function() { document.querySelectorAll('#example')[0] }
]) // => [40.000000000873115, 45.99999999481952, 115.00000000523869]
Works...everywhere! Tested in node, Chrome, FF, Opera, IE8
$ npm start
$ npm test