diff options
author | Brian White <mscdex@mscdex.net> | 2020-03-14 14:55:44 +0300 |
---|---|---|
committer | Brian White <mscdex@mscdex.net> | 2020-05-31 00:24:43 +0300 |
commit | c24b74a7abec0848484671771d250cfd961f128e (patch) | |
tree | 1e4fec15ee4de3ab4bc483f759322389d71932ec /lib/internal/util | |
parent | 3f32126fd554be32cb53a2458849697146145fda (diff) |
lib: improve debuglog() performance
PR-URL: https://github.com/nodejs/node/pull/32260
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/util')
-rw-r--r-- | lib/internal/util/debuglog.js | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/lib/internal/util/debuglog.js b/lib/internal/util/debuglog.js index 72eb797474a..fce2e2f0d8c 100644 --- a/lib/internal/util/debuglog.js +++ b/lib/internal/util/debuglog.js @@ -35,6 +35,8 @@ function emitWarningIfNeeded(set) { } } +function noop() {} + function debuglogImpl(set) { set = set.toUpperCase(); if (debugs[set] === undefined) { @@ -48,7 +50,7 @@ function debuglogImpl(set) { process.stderr.write(format('%s %s: %s\n', set, coloredPID, msg)); }; } else { - debugs[set] = null; + debugs[set] = noop; } } return debugs[set]; @@ -58,16 +60,17 @@ function debuglogImpl(set) { // so it needs to be called lazily in top scopes of internal modules // that may be loaded before these run time states are allowed to // be accessed. -function debuglog(set) { - let debug; - return function(...args) { - if (debug === undefined) { - // Only invokes debuglogImpl() when the debug function is - // called for the first time. - debug = debuglogImpl(set); - } - if (debug !== null) - debug(...args); +function debuglog(set, cb) { + let debug = (...args) => { + // Only invokes debuglogImpl() when the debug function is + // called for the first time. + debug = debuglogImpl(set); + if (typeof cb === 'function') + cb(debug); + debug(...args); + }; + return (...args) => { + debug(...args); }; } |