Skip to content

Commit f97a609

Browse files
gurgundayaduh95
authored andcommitted
console: optimize single-string logging
PR-URL: #60422 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent bbd7fdf commit f97a609

File tree

2 files changed

+105
-7
lines changed

2 files changed

+105
-7
lines changed

‎benchmark/console/log.js‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
// A console throughput benchmark.
4+
// Uses a custom Console with null Writable streams to avoid I/O latency.
5+
6+
constcommon=require('../common.js');
7+
const{ Writable }=require('stream');
8+
const{ Console }=require('console');
9+
10+
constbench=common.createBenchmark(main,{
11+
n: [2e6],
12+
variant: ['plain','format','object','group','info','warn','error'],
13+
});
14+
15+
classNullextendsWritable{
16+
_write(chunk,enc,cb){cb();}
17+
}
18+
19+
functionmakeConsole(){
20+
constdn=newNull();
21+
returnnewConsole({stdout: dn,stderr: dn,ignoreErrors: true,colorMode: false});
22+
}
23+
24+
functionmain({ n, variant }){
25+
constc=makeConsole();
26+
27+
switch(variant){
28+
case'plain': {
29+
bench.start();
30+
for(leti=0;i<n;i++)c.log('hello world');
31+
bench.end(n);
32+
break;
33+
}
34+
case'format': {
35+
bench.start();
36+
for(leti=0;i<n;i++)c.log('%s %d %j','a',42,{x: 1});
37+
bench.end(n);
38+
break;
39+
}
40+
case'object': {
41+
constobj={a: 1,b: 2,c: 3};
42+
bench.start();
43+
for(leti=0;i<n;i++)c.log(obj);
44+
bench.end(n);
45+
break;
46+
}
47+
case'group': {
48+
bench.start();
49+
for(leti=0;i<n;i++){
50+
c.group('g');
51+
c.log('x');
52+
c.groupEnd();
53+
}
54+
bench.end(n);
55+
break;
56+
}
57+
case'info': {
58+
bench.start();
59+
for(leti=0;i<n;i++)c.info('hello world');
60+
bench.end(n);
61+
break;
62+
}
63+
case'warn': {
64+
bench.start();
65+
for(leti=0;i<n;i++)c.warn('hello world');
66+
bench.end(n);
67+
break;
68+
}
69+
case'error': {
70+
bench.start();
71+
for(leti=0;i<n;i++)c.error('hello world');
72+
bench.end(n);
73+
break;
74+
}
75+
default:
76+
thrownewError('unknown variant');
77+
}
78+
}

‎lib/internal/console/constructor.js‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ ObjectDefineProperty(Console, SymbolHasInstance,{
190190
constkColorInspectOptions={colors: true};
191191
constkNoColorInspectOptions={};
192192

193-
constinternalIndentationMap=newSafeWeakMap();
193+
constkGroupIndentationString=Symbol('kGroupIndentationString');
194194

195195
ObjectDefineProperties(Console.prototype,{
196196
[kBindStreamsEager]: {
@@ -264,6 +264,11 @@ ObjectDefineProperties(Console.prototype,{
264264
...consolePropAttributes,
265265
value: groupIndentation,
266266
},
267+
[kGroupIndentationString]: {
268+
__proto__: null,
269+
...consolePropAttributes,
270+
value: '',
271+
},
267272
[SymbolToStringTag]: {
268273
__proto__: null,
269274
writable: false,
@@ -279,7 +284,7 @@ ObjectDefineProperties(Console.prototype,{
279284
...consolePropAttributes,
280285
value: function(streamSymbol,string){
281286
constignoreErrors=this._ignoreErrors;
282-
constgroupIndent=internalIndentationMap.get(this)||'';
287+
constgroupIndent=this[kGroupIndentationString];
283288

284289
constuseStdout=streamSymbol===kUseStdout;
285290
conststream=useStdout ? this._stdout : this._stderr;
@@ -342,6 +347,14 @@ ObjectDefineProperties(Console.prototype,{
342347
__proto__: null,
343348
...consolePropAttributes,
344349
value: function(args){
350+
if(args.length===1){
351+
// Fast path: single string, don't call format.
352+
// Avoids ReflectApply and validation overhead.
353+
consta0=args[0];
354+
if(typeofa0==='string'){
355+
returna0;
356+
}
357+
}
345358
constopts=this[kGetInspectOptions](this._stdout);
346359
ArrayPrototypeUnshift(args,opts);
347360
returnReflectApply(formatWithOptions,null,args);
@@ -351,6 +364,14 @@ ObjectDefineProperties(Console.prototype,{
351364
__proto__: null,
352365
...consolePropAttributes,
353366
value: function(args){
367+
if(args.length===1){
368+
// Fast path: single string, don't call format.
369+
// Avoids ReflectApply and validation overhead.
370+
consta0=args[0];
371+
if(typeofa0==='string'){
372+
returna0;
373+
}
374+
}
354375
constopts=this[kGetInspectOptions](this._stderr);
355376
ArrayPrototypeUnshift(args,opts);
356377
returnReflectApply(formatWithOptions,null,args);
@@ -513,21 +534,20 @@ const consoleMethods ={
513534
ReflectApply(this.log,this,data);
514535
}
515536

516-
letcurrentIndentation=internalIndentationMap.get(this)||'';
537+
letcurrentIndentation=this[kGroupIndentationString];
517538
currentIndentation+=StringPrototypeRepeat(' ',this[kGroupIndentationWidth]);
518-
519-
internalIndentationMap.set(this,currentIndentation);
539+
this[kGroupIndentationString]=currentIndentation;
520540
},
521541

522542
groupEnd(){
523-
constcurrentIndentation=internalIndentationMap.get(this)||'';
543+
constcurrentIndentation=this[kGroupIndentationString];
524544
constnewIndentation=StringPrototypeSlice(
525545
currentIndentation,
526546
0,
527547
currentIndentation.length-this[kGroupIndentationWidth],
528548
);
529549

530-
internalIndentationMap.set(this,newIndentation);
550+
this[kGroupIndentationString]=newIndentation;
531551
},
532552

533553
// https://console.spec.whatwg.org/#table

0 commit comments

Comments
(0)