Skip to content

Commit d628f3a

Browse files
not-an-aardvarkitaloacasas
authored andcommitted
util: avoid out-of-bounds arguments index access
This updates util.inspect() to avoid accessing out-of-range indices of the `arguments` object, which is known to cause optimization bailout. Based on an average of 10 runs of the benchmark in `benchmark/util/inspect.js`, this change improves the performance of `util.inspect` by about 10%. Relates to #10323 PR-URL: #10569 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Jackson Tian <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b2d0c44 commit d628f3a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/util.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,12 @@ function inspect(obj, opts) {
173173
stylize: stylizeNoColor
174174
};
175175
// legacy...
176-
if (arguments[2] !== undefined) ctx.depth = arguments[2];
177-
if (arguments[3] !== undefined) ctx.colors = arguments[3];
176+
if (arguments.length >= 3 && arguments[2] !== undefined) {
177+
ctx.depth = arguments[2];
178+
}
179+
if (arguments.length >= 4 && arguments[3] !== undefined) {
180+
ctx.colors = arguments[3];
181+
}
178182
if (typeof opts === 'boolean') {
179183
// legacy...
180184
ctx.showHidden = opts;

0 commit comments

Comments
 (0)