Created
July 19, 2019 22:57
-
-
Save bericp1/38753159d8d98bfe80370f085f845f1e to your computer and use it in GitHub Desktop.
Creates a high resolution timer. Call with a name to start. Call with a new name to "lap". Call with no arguments to stop.
This file contains hidden or 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 createTimer (log) { | |
let prev = null; | |
function hrtimeDurationToMs([durS, durUs]) { | |
return ((durS * 1000) + (durUs / 1000000)).toFixed(4); | |
} | |
return (name = null) => { | |
if (prev) { | |
const dur = hrtimeDurationToMs(process.hrtime(prev[1])); | |
log(`end ${prev[0]}, took ${dur}ms`, { name: prev[0], hrtime: prev[1], dur }); | |
} | |
if (name) { | |
const hrtime = process.hrtime(); | |
prev = [name, hrtime]; | |
log(`start ${name}`, { name, hrtime }); | |
} else { | |
prev = null; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment