util: avoid run debug when enabled is false

it can improve performace 700x faster

PR-URL: https://github.com/nodejs/node/pull/57494
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
fengmk2
2025-03-21 08:00:52 +08:00
committed by GitHub
parent 96403793a1
commit afe3909483

View File

@@ -62,7 +62,7 @@ function lazyUtilColors() {
return utilColors;
}
function debuglogImpl(enabled, set, args) {
function debuglogImpl(enabled, set) {
if (debugImpls[set] === undefined) {
if (enabled) {
const pid = process.pid;
@@ -109,6 +109,8 @@ function debuglog(set, cb) {
return enabled;
};
const logger = (...args) => {
// Improve performance when debug is disabled, avoid calling `new SafeArrayIterator(args)`
if (enabled === false) return;
switch (args.length) {
case 1: return debug(args[0]);
case 2: return debug(args[0], args[1]);