From a309ee11ca8284f07b2efa6f2835b9dab2c37303 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 30 Nov 2019 11:04:34 +0100 Subject: [PATCH] util: improve performance inspecting proxies This makes sure we do not retrieve the handler in case it's not required. This improves the performance a tiny bit for these cases. Backport-PR-URL: https://github.com/nodejs/node/pull/31431 PR-URL: https://github.com/nodejs/node/pull/30767 Reviewed-By: James M Snell Reviewed-By: Denys Otrishko Reviewed-By: Rich Trott --- benchmark/util/inspect-proxy.js | 2 +- lib/internal/util/inspect.js | 4 ++-- src/node_util.cc | 20 ++++++++++++++------ test/parallel/test-util-inspect-proxy.js | 7 +++++-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/benchmark/util/inspect-proxy.js b/benchmark/util/inspect-proxy.js index fd89d568aba35f..02379cdc770f3b 100644 --- a/benchmark/util/inspect-proxy.js +++ b/benchmark/util/inspect-proxy.js @@ -4,7 +4,7 @@ const util = require('util'); const common = require('../common.js'); const bench = common.createBenchmark(main, { - n: [2e4], + n: [1e5], showProxy: [0, 1], isProxy: [0, 1] }); diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index da6f9426ec7b25..09f755ac00649f 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -640,12 +640,12 @@ function formatValue(ctx, value, recurseTimes, typedArray) { const context = value; // Always check for proxies to prevent side effects and to prevent triggering // any proxy handlers. - const proxy = getProxyDetails(value); + const proxy = getProxyDetails(value, !!ctx.showProxy); if (proxy !== undefined) { if (ctx.showProxy) { return formatProxy(ctx, proxy, recurseTimes); } - value = proxy[0]; + value = proxy; } // Provide a hook for user-specified inspect functions. diff --git a/src/node_util.cc b/src/node_util.cc index 4d0b109371fb60..b9553eaaa6763d 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -91,15 +91,23 @@ static void GetProxyDetails(const FunctionCallbackInfo& args) { if (!args[0]->IsProxy()) return; + CHECK(args[1]->IsBoolean()); + Local proxy = args[0].As(); - Local ret[] = { - proxy->GetTarget(), - proxy->GetHandler() - }; + if (args[1]->IsTrue()) { + Local ret[] = { + proxy->GetTarget(), + proxy->GetHandler() + }; - args.GetReturnValue().Set( - Array::New(args.GetIsolate(), ret, arraysize(ret))); + args.GetReturnValue().Set( + Array::New(args.GetIsolate(), ret, arraysize(ret))); + } else { + Local ret = proxy->GetTarget(); + + args.GetReturnValue().Set(ret); + } } static void PreviewEntries(const FunctionCallbackInfo& args) { diff --git a/test/parallel/test-util-inspect-proxy.js b/test/parallel/test-util-inspect-proxy.js index c20af7450a426e..5e85b92f713175 100644 --- a/test/parallel/test-util-inspect-proxy.js +++ b/test/parallel/test-util-inspect-proxy.js @@ -43,10 +43,13 @@ util.inspect(proxyObj, opts); // getProxyDetails is an internal method, not intended for public use. // This is here to test that the internals are working correctly. -const details = processUtil.getProxyDetails(proxyObj); +let details = processUtil.getProxyDetails(proxyObj, true); assert.strictEqual(target, details[0]); assert.strictEqual(handler, details[1]); +details = processUtil.getProxyDetails(proxyObj, false); +assert.strictEqual(target, details); + assert.strictEqual( util.inspect(proxyObj, opts), 'Proxy [\n' + @@ -105,7 +108,7 @@ const expected6 = 'Proxy [\n' + ' ]\n' + ']'; assert.strictEqual( - util.inspect(proxy1, { showProxy: true, depth: null }), + util.inspect(proxy1, { showProxy: 1, depth: null }), expected1); assert.strictEqual(util.inspect(proxy2, opts), expected2); assert.strictEqual(util.inspect(proxy3, opts), expected3);