Skip to content

Commit a309ee1

Browse files
BridgeARMylesBorins
authored andcommitted
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: #31431 PR-URL: #30767 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent a1555cb commit a309ee1

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

benchmark/util/inspect-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const util = require('util');
44
const common = require('../common.js');
55

66
const bench = common.createBenchmark(main, {
7-
n: [2e4],
7+
n: [1e5],
88
showProxy: [0, 1],
99
isProxy: [0, 1]
1010
});

lib/internal/util/inspect.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,12 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
640640
const context = value;
641641
// Always check for proxies to prevent side effects and to prevent triggering
642642
// any proxy handlers.
643-
const proxy = getProxyDetails(value);
643+
const proxy = getProxyDetails(value, !!ctx.showProxy);
644644
if (proxy !== undefined) {
645645
if (ctx.showProxy) {
646646
return formatProxy(ctx, proxy, recurseTimes);
647647
}
648-
value = proxy[0];
648+
value = proxy;
649649
}
650650

651651
// Provide a hook for user-specified inspect functions.

src/node_util.cc

+14-6
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,23 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
9191
if (!args[0]->IsProxy())
9292
return;
9393

94+
CHECK(args[1]->IsBoolean());
95+
9496
Local<Proxy> proxy = args[0].As<Proxy>();
9597

96-
Local<Value> ret[] = {
97-
proxy->GetTarget(),
98-
proxy->GetHandler()
99-
};
98+
if (args[1]->IsTrue()) {
99+
Local<Value> ret[] = {
100+
proxy->GetTarget(),
101+
proxy->GetHandler()
102+
};
100103

101-
args.GetReturnValue().Set(
102-
Array::New(args.GetIsolate(), ret, arraysize(ret)));
104+
args.GetReturnValue().Set(
105+
Array::New(args.GetIsolate(), ret, arraysize(ret)));
106+
} else {
107+
Local<Value> ret = proxy->GetTarget();
108+
109+
args.GetReturnValue().Set(ret);
110+
}
103111
}
104112

105113
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {

test/parallel/test-util-inspect-proxy.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ util.inspect(proxyObj, opts);
4343

4444
// getProxyDetails is an internal method, not intended for public use.
4545
// This is here to test that the internals are working correctly.
46-
const details = processUtil.getProxyDetails(proxyObj);
46+
let details = processUtil.getProxyDetails(proxyObj, true);
4747
assert.strictEqual(target, details[0]);
4848
assert.strictEqual(handler, details[1]);
4949

50+
details = processUtil.getProxyDetails(proxyObj, false);
51+
assert.strictEqual(target, details);
52+
5053
assert.strictEqual(
5154
util.inspect(proxyObj, opts),
5255
'Proxy [\n' +
@@ -105,7 +108,7 @@ const expected6 = 'Proxy [\n' +
105108
' ]\n' +
106109
']';
107110
assert.strictEqual(
108-
util.inspect(proxy1, { showProxy: true, depth: null }),
111+
util.inspect(proxy1, { showProxy: 1, depth: null }),
109112
expected1);
110113
assert.strictEqual(util.inspect(proxy2, opts), expected2);
111114
assert.strictEqual(util.inspect(proxy3, opts), expected3);

0 commit comments

Comments
 (0)