Created
February 2, 2018 02:18
-
-
Save nkbt/3091d11e77513dc1469ff1381f16797f to your computer and use it in GitHub Desktop.
Namespaced perf measurer
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
const perfCache = {start: {}, end: {}}; | |
const perf = key => { | |
if (key in perfCache.end) { | |
return perfCache.end[key]; | |
} | |
if (key in perfCache.start) { | |
const [s, ns] = process.hrtime(perfCache.start[key]); | |
Object.assign(perfCache.end, {[key]: (s + ns / 1e9)}); | |
return perfCache.end[key]; | |
} | |
Object.assign(perfCache.start, {[key]: process.hrtime()}); | |
return perfCache.start[key]; | |
}; | |
perf('whatever'); | |
setTimeout(() => console.log(perf('whatever')), 500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment