From bdaca4a0d30499c105195259994eb1519a7084ec Mon Sep 17 00:00:00 2001 From: Florrie Date: Mon, 16 Mar 2020 14:26:14 -0300 Subject: now this commit is big --- upd8-util.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'upd8-util.js') diff --git a/upd8-util.js b/upd8-util.js index 82200ee..b24b3b7 100644 --- a/upd8-util.js +++ b/upd8-util.js @@ -120,3 +120,38 @@ module.exports.cacheOneArg = function (fn) { return arg[symbol]; }; }; + +const decorateTime = function (functionToBeWrapped) { + const fn = function(...args) { + const start = Date.now(); + const ret = functionToBeWrapped(...args); + const end = Date.now(); + fn.timeSpent += end - start; + fn.timesCalled++; + return ret; + }; + + fn.wrappedName = functionToBeWrapped.name; + fn.timeSpent = 0; + fn.timesCalled = 0; + fn.displayTime = function() { + const averageTime = fn.timeSpent / fn.timesCalled; + console.log(`\x1b[1m${fn.wrappedName}(...):\x1b[0m ${fn.timeSpent} ms / ${fn.timesCalled} calls \x1b[2m(avg: ${averageTime} ms)\x1b[0m`); + }; + + decorateTime.decoratedFunctions.push(fn); + + return fn; +}; + +decorateTime.decoratedFunctions = []; +decorateTime.displayTime = function() { + if (decorateTime.decoratedFunctions.length) { + console.log(`\x1b[1mdecorateTime results: ` + '-'.repeat(40) + '\x1b[0m'); + for (const fn of decorateTime.decoratedFunctions) { + fn.displayTime(); + } + } +}; + +module.exports.decorateTime = decorateTime; -- cgit 1.3.0-6-gf8a5