console: optimize single-string logging · nodejs/node@94a94a6
@@ -190,7 +190,7 @@ ObjectDefineProperty(Console, SymbolHasInstance, {
190190const kColorInspectOptions = { colors: true };
191191const kNoColorInspectOptions = {};
192192193-const internalIndentationMap = new SafeWeakMap();
193+const kGroupIndentationString = Symbol('kGroupIndentationString');
194194195195ObjectDefineProperties(Console.prototype, {
196196[kBindStreamsEager]: {
@@ -264,6 +264,11 @@ ObjectDefineProperties(Console.prototype, {
264264 ...consolePropAttributes,
265265value: groupIndentation,
266266},
267+[kGroupIndentationString]: {
268+__proto__: null,
269+ ...consolePropAttributes,
270+value: '',
271+},
267272[SymbolToStringTag]: {
268273__proto__: null,
269274writable: false,
@@ -279,7 +284,7 @@ ObjectDefineProperties(Console.prototype, {
279284 ...consolePropAttributes,
280285value: function(streamSymbol, string) {
281286const ignoreErrors = this._ignoreErrors;
282-const groupIndent = internalIndentationMap.get(this) || '';
287+const groupIndent = this[kGroupIndentationString];
283288284289const useStdout = streamSymbol === kUseStdout;
285290const stream = useStdout ? this._stdout : this._stderr;
@@ -342,6 +347,14 @@ ObjectDefineProperties(Console.prototype, {
342347__proto__: null,
343348 ...consolePropAttributes,
344349value: 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+}
345358const opts = this[kGetInspectOptions](this._stdout);
346359ArrayPrototypeUnshift(args, opts);
347360return ReflectApply(formatWithOptions, null, args);
@@ -351,6 +364,14 @@ ObjectDefineProperties(Console.prototype, {
351364__proto__: null,
352365 ...consolePropAttributes,
353366value: 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+}
354375const opts = this[kGetInspectOptions](this._stderr);
355376ArrayPrototypeUnshift(args, opts);
356377return ReflectApply(formatWithOptions, null, args);
@@ -513,21 +534,20 @@ const consoleMethods = {
513534ReflectApply(this.log, this, data);
514535}
515536516-let currentIndentation = internalIndentationMap.get(this) || '';
537+let currentIndentation = this[kGroupIndentationString];
517538currentIndentation += StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
518-519-internalIndentationMap.set(this, currentIndentation);
539+this[kGroupIndentationString] = currentIndentation;
520540},
521541522542groupEnd() {
523-const currentIndentation = internalIndentationMap.get(this) || '';
543+const currentIndentation = this[kGroupIndentationString];
524544const newIndentation = StringPrototypeSlice(
525545currentIndentation,
5265460,
527547currentIndentation.length - this[kGroupIndentationWidth],
528548);
529549530-internalIndentationMap.set(this, newIndentation);
550+this[kGroupIndentationString] = newIndentation;
531551},
532552533553// https://console.spec.whatwg.org/#table