Skip to content

Commit 9752fce

Browse files
committed
util: improve format performance
This simplifies the `format()` code and significantly improves the performance. PR-URL: #24981 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent d0c240f commit 9752fce

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

lib/util.js

+40-45
Original file line numberDiff line numberDiff line change
@@ -78,45 +78,32 @@ function format(...args) {
7878
return formatWithOptions(emptyOptions, ...args);
7979
}
8080

81-
function formatValue(val, inspectOptions) {
82-
const inspectTypes = ['object', 'symbol', 'function', 'number'];
83-
84-
if (inspectTypes.includes(typeof val)) {
85-
return inspect(val, inspectOptions);
86-
} else {
87-
return String(val);
88-
}
89-
}
90-
9181
function formatWithOptions(inspectOptions, ...args) {
9282
const first = args[0];
93-
const parts = [];
83+
let a = 0;
84+
let str = '';
85+
let join = '';
9486

95-
const firstIsString = typeof first === 'string';
96-
97-
if (firstIsString && args.length === 1) {
98-
return first;
99-
}
100-
101-
if (firstIsString && /%[sjdOoif%]/.test(first)) {
102-
let i, tempStr;
103-
let str = '';
104-
let a = 1;
87+
if (typeof first === 'string') {
88+
if (args.length === 1) {
89+
return first;
90+
}
91+
let tempStr;
10592
let lastPos = 0;
10693

107-
for (i = 0; i < first.length - 1; i++) {
94+
for (var i = 0; i < first.length - 1; i++) {
10895
if (first.charCodeAt(i) === 37) { // '%'
10996
const nextChar = first.charCodeAt(++i);
110-
if (a !== args.length) {
97+
if (a + 1 !== args.length) {
11198
switch (nextChar) {
11299
case 115: // 's'
113-
tempStr = String(args[a++]);
100+
tempStr = String(args[++a]);
114101
break;
115102
case 106: // 'j'
116-
tempStr = tryStringify(args[a++]);
103+
tempStr = tryStringify(args[++a]);
117104
break;
118105
case 100: // 'd'
119-
const tempNum = args[a++];
106+
const tempNum = args[++a];
120107
// eslint-disable-next-line valid-typeof
121108
if (typeof tempNum === 'bigint') {
122109
tempStr = `${tempNum}n`;
@@ -127,20 +114,20 @@ function formatWithOptions(inspectOptions, ...args) {
127114
}
128115
break;
129116
case 79: // 'O'
130-
tempStr = inspect(args[a++], inspectOptions);
117+
tempStr = inspect(args[++a], inspectOptions);
131118
break;
132119
case 111: // 'o'
133120
{
134-
const opts = Object.assign({}, inspectOptions, {
121+
tempStr = inspect(args[++a], {
122+
...inspectOptions,
135123
showHidden: true,
136124
showProxy: true,
137125
depth: 4
138126
});
139-
tempStr = inspect(args[a++], opts);
140127
break;
141128
}
142129
case 105: // 'i'
143-
const tempInteger = args[a++];
130+
const tempInteger = args[++a];
144131
// eslint-disable-next-line valid-typeof
145132
if (typeof tempInteger === 'bigint') {
146133
tempStr = `${tempInteger}n`;
@@ -151,7 +138,7 @@ function formatWithOptions(inspectOptions, ...args) {
151138
}
152139
break;
153140
case 102: // 'f'
154-
const tempFloat = args[a++];
141+
const tempFloat = args[++a];
155142
if (typeof tempFloat === 'symbol') {
156143
tempStr = 'NaN';
157144
} else {
@@ -176,24 +163,32 @@ function formatWithOptions(inspectOptions, ...args) {
176163
}
177164
}
178165
}
179-
if (lastPos === 0) {
180-
str = first;
181-
} else if (lastPos < first.length) {
182-
str += first.slice(lastPos);
183-
}
184-
185-
parts.push(str);
186-
while (a < args.length) {
187-
parts.push(formatValue(args[a], inspectOptions));
166+
if (lastPos !== 0) {
188167
a++;
189-
}
190-
} else {
191-
for (const arg of args) {
192-
parts.push(formatValue(arg, inspectOptions));
168+
join = ' ';
169+
if (lastPos < first.length) {
170+
str += first.slice(lastPos);
171+
}
193172
}
194173
}
195174

196-
return parts.join(' ');
175+
while (a < args.length) {
176+
const value = args[a];
177+
// TODO(BridgeAR): This should apply for all besides strings. Especially
178+
// BigInt should be properly inspected.
179+
str += join;
180+
if (typeof value !== 'string' &&
181+
typeof value !== 'boolean' &&
182+
// eslint-disable-next-line valid-typeof
183+
typeof value !== 'bigint') {
184+
str += inspect(value, inspectOptions);
185+
} else {
186+
str += value;
187+
}
188+
join = ' ';
189+
a++;
190+
}
191+
return str;
197192
}
198193

199194
const debugs = {};

0 commit comments

Comments
 (0)