console: optimize single-string logging · nodejs/node@94a94a6

@@ -190,7 +190,7 @@ ObjectDefineProperty(Console, SymbolHasInstance, {

190190

const kColorInspectOptions = { colors: true };

191191

const kNoColorInspectOptions = {};

192192193-

const internalIndentationMap = new SafeWeakMap();

193+

const kGroupIndentationString = Symbol('kGroupIndentationString');

194194195195

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

const ignoreErrors = this._ignoreErrors;

282-

const groupIndent = internalIndentationMap.get(this) || '';

287+

const groupIndent = this[kGroupIndentationString];

283288284289

const useStdout = streamSymbol === kUseStdout;

285290

const stream = 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+

const a0 = args[0];

354+

if (typeof a0 === 'string') {

355+

return a0;

356+

}

357+

}

345358

const opts = this[kGetInspectOptions](this._stdout);

346359

ArrayPrototypeUnshift(args, opts);

347360

return ReflectApply(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+

const a0 = args[0];

371+

if (typeof a0 === 'string') {

372+

return a0;

373+

}

374+

}

354375

const opts = this[kGetInspectOptions](this._stderr);

355376

ArrayPrototypeUnshift(args, opts);

356377

return ReflectApply(formatWithOptions, null, args);

@@ -513,21 +534,20 @@ const consoleMethods = {

513534

ReflectApply(this.log, this, data);

514535

}

515536516-

let currentIndentation = internalIndentationMap.get(this) || '';

537+

let currentIndentation = this[kGroupIndentationString];

517538

currentIndentation += StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);

518-519-

internalIndentationMap.set(this, currentIndentation);

539+

this[kGroupIndentationString] = currentIndentation;

520540

},

521541522542

groupEnd() {

523-

const currentIndentation = internalIndentationMap.get(this) || '';

543+

const currentIndentation = this[kGroupIndentationString];

524544

const newIndentation = StringPrototypeSlice(

525545

currentIndentation,

526546

0,

527547

currentIndentation.length - this[kGroupIndentationWidth],

528548

);

529549530-

internalIndentationMap.set(this, newIndentation);

550+

this[kGroupIndentationString] = newIndentation;

531551

},

532552533553

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