« get me outta code hell

now this commit is big - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/upd8-util.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-03-16 14:26:14 -0300
committerFlorrie <towerofnix@gmail.com>2020-03-16 14:26:14 -0300
commitbdaca4a0d30499c105195259994eb1519a7084ec (patch)
treea51f59e8e22a440662abed9bb3dc9be24b65fe8d /upd8-util.js
parent5d7b838a030f8bdab66c5b8a48cc30c9b23b6737 (diff)
now this commit is big
Diffstat (limited to 'upd8-util.js')
-rw-r--r--upd8-util.js35
1 files changed, 35 insertions, 0 deletions
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;