Skip to content

Instantly share code, notes, and snippets.

@nkbt
Created February 2, 2018 02:18
Show Gist options
  • Save nkbt/3091d11e77513dc1469ff1381f16797f to your computer and use it in GitHub Desktop.
Save nkbt/3091d11e77513dc1469ff1381f16797f to your computer and use it in GitHub Desktop.
Namespaced perf measurer
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