Skip to content

Commit eeaeb51

Browse files
BridgeARtargos
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. PR-URL: #30767 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent ad4d52d commit eeaeb51

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

‎benchmark/util/inspect-proxy.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const util = require('util');
44
constcommon=require('../common.js');
55

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

‎lib/internal/util/inspect.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,12 +638,12 @@ function formatValue(ctx, value, recurseTimes, typedArray){
638638
constcontext=value;
639639
// Always check for proxies to prevent side effects and to prevent triggering
640640
// any proxy handlers.
641-
constproxy=getProxyDetails(value);
641+
constproxy=getProxyDetails(value,!!ctx.showProxy);
642642
if(proxy!==undefined){
643643
if(ctx.showProxy){
644644
returnformatProxy(ctx,proxy,recurseTimes);
645645
}
646-
value=proxy[0];
646+
value=proxy;
647647
}
648648

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

‎src/node_util.cc‎

Lines changed: 14 additions & 6 deletions
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
staticvoidPreviewEntries(const FunctionCallbackInfo<Value>& args){

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

Lines changed: 5 additions & 2 deletions
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-
constdetails=processUtil.getProxyDetails(proxyObj);
46+
letdetails=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)