Last active
August 14, 2016 08:14
-
-
Save TrySound/72caea2645bc6bc9313b97c14b75480f to your computer and use it in GitHub Desktop.
Benchmark tool
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
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