Skip to content

Instantly share code, notes, and snippets.

@TrySound
Last active August 14, 2016 08:14
Show Gist options
  • Save TrySound/72caea2645bc6bc9313b97c14b75480f to your computer and use it in GitHub Desktop.
Save TrySound/72caea2645bc6bc9313b97c14b75480f to your computer and use it in GitHub Desktop.
Benchmark tool
var map = {};
var list = [];
function timeStart(label) {
if (!map[label]) {
list.push(label);
map[label] = {
time: 0
};
}
map[label].start = process.hrtime();
}
function timeEnd(label) {
if (map[label]) {
map[label].time += toMilliseconds(process.hrtime(map[label].start));
}
}
function flushTime(log) {
if (!log) {
log = defaultLog
}
list.forEach(function (label) {
if ('time' in map[label]) {
log(label, map[label].time);
}
});
map = {};
list = [];
}
function toMilliseconds(time) {
return time[0] * 1e+3 + Math.floor(time[1] * 1e-6);
}
function defaultLog(label, time) {
console.log('%dms: %s', time, label);
}
exports.timeStart = timeStart;
exports.timeEnd = timeEnd;
exports.flushTime = flushTime;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment