Repository: nodejs/node
Pull request: util: avoid run debug when enabled is false

Once there are more than 2 inputs to the `debug logger`, e.g.: `debug(1, 2, 3)`,
there is a significant performance degradation.

This change can improve performance 700x faster.

## Benchmark

Test script

```js
import { debuglog } from 'node:util';
import { run, bench, boxplot, summary } from 'mitata';

const debug = debuglog('test');

boxplot(() => {
  summary(() => {
    bench('debug()', () => {
      debug();
    });

    bench('debug(arg1)', () => {
      debug('arg1');
    });

    bench('debug(arg1, arg2)', () => {
      debug('arg1', 'arg2');
    });

    bench('debug(arg1, arg2, arg3)', () => {
      debug('arg1', 'arg2', 'arg3');
    });

    bench('debug(arg1, arg2, arg3, arg4)', () => {
      debug('arg1', 'arg2', 'arg3', 'arg4');
    });

    bench('debug(arg1, arg2, arg3, arg4, arg5)', () => {
      debug('arg1', 'arg2', 'arg3', 'arg4', 'arg5');
    });

    bench('debug(arg1, arg2, arg3, arg4, arg5, arg6)', () => {
      debug('arg1', 'arg2', 'arg3', 'arg4', 'arg5', 'arg6');
    });
  });
});

await run();
```

Before

```bash
clk: ~2.77 GHz
cpu: Apple M1
runtime: node 22.14.0 (arm64-darwin)

benchmark                                avg (min … max) p75 / p99    (min … top 1%)
-------------------------------------------------------- -------------------------------
debug()                                    48.10 ns/iter  46.31 ns █▇                   
                                  (44.68 ns … 537.07 ns)  77.05 ns ██                   
                                 ( 95.53  b … 256.21  b) 184.23  b ██▄▂▂▁▁▁▁▁▁▁▁▁▁▂▂▂▁▁▁

debug(arg1)                                 2.49 ns/iter   2.18 ns █                    
                                    (1.66 ns … 53.58 ns)  26.13 ns █                    
                                 ( 33.25  b …  88.13  b)  56.15  b █▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

debug(arg1, arg2)                           2.96 ns/iter   2.56 ns █                    
                                    (1.95 ns … 83.79 ns)  27.63 ns █                    
                                 (  5.38  b …  96.13  b)  64.14  b █▆▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

debug(arg1, arg2, arg3)                   134.43 ns/iter 129.98 ns  █                   
                                 (125.27 ns … 645.71 ns) 202.32 ns ▃█                   
                                 (378.88  b … 683.47  b) 528.22  b ██▃▂▁▁▂▄▂▁▁▁▁▁▁▁▁▁▁▁▁

debug(arg1, arg2, arg3, arg4)             175.44 ns/iter 176.32 ns     █                
                                   (156.77 ns … 1.29 µs) 221.29 ns   ▆▅█                
                                 (435.13  b … 702.23  b) 584.06  b ▅▂████▅▅▂▂▂▂▃▂▃▂▁▂▁▁▁

debug(arg1, arg2, arg3, arg4, arg5)       189.31 ns/iter 189.73 ns   █                  
                                 (179.46 ns … 299.87 ns) 225.29 ns ▇▅█                  
                                 (505.08  b … 663.07  b) 639.79  b ████▇▄▃▂▂▁▁▃▄▄▃▂▂▂▂▁▁

debug(arg1, arg2, arg3, arg4, arg5, arg6) 209.56 ns/iter 212.41 ns █                    
                                 (201.88 ns … 266.42 ns) 247.06 ns █▆                   
                                 (334.36  b … 856.20  b) 695.85  b ██▄▂▂▂▂▁▁▁▁▄▄▃▁▁▁▁▁▁▁

                                          ┌                                            ┐
                                                  ┌┬    ╷
                                  debug()         ││────┤
                                                  └┴    ╵
                                          ┬   ╷
                              debug(arg1) │───┤
                                          ┴   ╵
                                          ┬    ╷
                        debug(arg1, arg2) │────┤
                                          ┴    ╵
                                                                 ┌┬            ╷
                  debug(arg1, arg2, arg3)                        ││────────────┤
                                                                 └┴            ╵
                                                                      ╷ ┌─┬       ╷
            debug(arg1, arg2, arg3, arg4)                             ├─┤ │───────┤
                                                                      ╵ └─┴       ╵
                                                                           ┌┬      ╷
      debug(arg1, arg2, arg3, arg4, arg5)                                  ││──────┤
                                                                           └┴      ╵
                                                                               ┌┬┐     ╷
debug(arg1, arg2, arg3, arg4, arg5, arg6)                                      ││├─────┤
                                                                               └┴┘     ╵
                                          └                                            ┘
                                          1.66 ns          124.36 ns           247.06 ns

summary
  debug(arg1)
   1.19x faster than debug(arg1, arg2)
   19.32x faster than debug()
   54.01x faster than debug(arg1, arg2, arg3)
   70.48x faster than debug(arg1, arg2, arg3, arg4)
   76.06x faster than debug(arg1, arg2, arg3, arg4, arg5)
   84.19x faster than debug(arg1, arg2, arg3, arg4, arg5, arg6)
```

After

```bash
clk: ~3.08 GHz
cpu: Apple M1
runtime: node 24.0.0-pre (arm64-darwin)

benchmark                                avg (min … max) p75 / p99    (min … top 1%)
-------------------------------------------------------- -------------------------------
debug()                                   165.97 ps/iter 162.84 ps              █       
                                  (142.33 ps … 13.80 ns) 173.10 ps              █       
                                 (  0.03  b …  53.95  b)   0.09  b ▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▄

debug(arg1)                               167.87 ps/iter 172.85 ps           █          
                                  (142.33 ps … 14.18 ns) 183.11 ps           █    ▂     
                                 (  0.09  b …  23.35  b)   0.09  b ▁▁▁▁▁▁▁▁▁▁█▁▁▁▁█▁▁▁▁▁

debug(arg1, arg2)                         164.65 ps/iter 162.84 ps              █       
                                  (142.33 ps … 12.07 ns) 173.10 ps              █       
                                 (  0.09  b …  23.60  b)   0.09  b ▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▂

debug(arg1, arg2, arg3)                   165.38 ps/iter 162.84 ps           █          
                                  (142.33 ps … 24.65 ns) 183.11 ps           █          
                                 (  0.09  b …  23.60  b)   0.09  b ▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▂▁▁▁▁▁

debug(arg1, arg2, arg3, arg4)             167.57 ps/iter 172.85 ps           █          
                                  (142.33 ps … 10.47 ns) 183.11 ps           █    ▂     
                                 (  0.09  b …  76.49  b)   0.09  b ▁▁▁▁▁▁▁▁▁▁█▁▁▁▁█▁▁▁▁▁

debug(arg1, arg2, arg3, arg4, arg5)       165.38 ps/iter 162.84 ps           █          
                                  (142.33 ps … 30.79 ns) 183.11 ps           █          
                                 (  0.09  b …  28.22  b)   0.09  b ▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▂▁▁▁▁▁

debug(arg1, arg2, arg3, arg4, arg5, arg6) 166.64 ps/iter 162.84 ps         █            
                                  (142.33 ps … 10.51 ns) 193.12 ps         █            
                                 (  0.10  b …  31.22  b)   0.10  b ▁▁▁▁▁▁▁▁█▁▁▁▆▁▁▁▁▁▁▁▁

                                          ┌                                            ┐
                                          ╷                 ┌──┬     ╷
                                  debug() ├─────────────────┤  │─────┤
                                          ╵                 └──┴     ╵
                                          ╷                 ┌────┬───┐        ╷
                              debug(arg1) ├─────────────────┤    │   ├────────┤
                                          ╵                 └────┴───┘        ╵
                                          ╷                 ┌─┬      ╷
                        debug(arg1, arg2) ├─────────────────┤ │──────┤
                                          ╵                 └─┴      ╵
                                          ╷                 ┌─┬               ╷
                  debug(arg1, arg2, arg3) ├─────────────────┤ │───────────────┤
                                          ╵                 └─┴               ╵
                                          ╷                 ┌───┬────┐        ╷
            debug(arg1, arg2, arg3, arg4) ├─────────────────┤   │    ├────────┤
                                          ╵                 └───┴────┘        ╵
                                          ╷                 ┌─┬               ╷
      debug(arg1, arg2, arg3, arg4, arg5) ├─────────────────┤ │───────────────┤
                                          ╵                 └─┴               ╵
                                          ╷                 ┌───┬                      ╷
debug(arg1, arg2, arg3, arg4, arg5, arg6) ├─────────────────┤   │──────────────────────┤
                                          ╵                 └───┴                      ╵
                                          └                                            ┘
                                          142.33 ps         167.72 ps          193.12 ps

summary
  debug(arg1, arg2)
   1x faster than debug(arg1, arg2, arg3)
   1x faster than debug(arg1, arg2, arg3, arg4, arg5)
   1.01x faster than debug()
   1.01x faster than debug(arg1, arg2, arg3, arg4, arg5, arg6)
   1.02x faster than debug(arg1, arg2, arg3, arg4)
   1.02x faster than debug(arg1)
```

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: lib/internal/util/debuglog.js
patch: @@ -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]);