From d628f3a227d8a76708e94d7acee70dbcbc0d85d2 Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Sun, 1 Jan 2017 19:42:19 -0500 Subject: [PATCH] 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 https://github.com/nodejs/node/issues/10323 PR-URL: https://github.com/nodejs/node/pull/10569 Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas Reviewed-By: Brian White Reviewed-By: Jackson Tian Reviewed-By: Jeremiah Senkpiel Reviewed-By: James M Snell --- lib/util.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 37008b2d176062..85af547ebb81f7 100644 --- a/lib/util.js +++ b/lib/util.js @@ -173,8 +173,12 @@ function inspect(obj, opts) { stylize: stylizeNoColor }; // legacy... - if (arguments[2] !== undefined) ctx.depth = arguments[2]; - if (arguments[3] !== undefined) ctx.colors = arguments[3]; + if (arguments.length >= 3 && arguments[2] !== undefined) { + ctx.depth = arguments[2]; + } + if (arguments.length >= 4 && arguments[3] !== undefined) { + ctx.colors = arguments[3]; + } if (typeof opts === 'boolean') { // legacy... ctx.showHidden = opts;