@@ -408,12 +408,15 @@ function printer(
408408 }
409409 }
410410
411- // Check string length budget:
412- // accumulate output length and if exceeded,
413- // force no further recursion by patching maxDepth.
414- // Inspired by Node's util.inspect bail out approach.
415- config . outputLength += result . length
416- if ( config . outputLength > config . maxOutputLength ) {
411+ // Per-depth output budget (inspired by Node's util.inspect).
412+ // Each depth level tracks output independently, so nested results
413+ // don't inflate a single counter (which would undercount by ~Nx for
414+ // N levels of nesting). Nodes at the same depth produce disjoint spans
415+ // in the output string, so each bucket accurately reflects output at
416+ // that level. Total output is bounded by maxDepth × maxOutputLength.
417+ config . _outputLengthPerDepth [ depth ] ??= 0
418+ config . _outputLengthPerDepth [ depth ] += result . length
419+ if ( config . _outputLengthPerDepth [ depth ] > config . maxOutputLength ) {
417420 config . maxDepth = 0
418421 }
419422
@@ -528,7 +531,7 @@ function getConfig(options?: OptionsReceived): Config {
528531 spacingInner : options ?. min ? ' ' : '\n' ,
529532 spacingOuter : options ?. min ? '' : '\n' ,
530533 maxOutputLength : options ?. maxOutputLength ?? DEFAULT_OPTIONS . maxOutputLength ,
531- outputLength : 0 ,
534+ _outputLengthPerDepth : [ ] ,
532535 }
533536}
534537
0 commit comments