Skip to content

Commit 354b1fa

Browse files
arturovtthePunderWoman
authored andcommitted
refactor(core): improve stringify (#59745)
In this commit, we improve branching in the `stringify` function, which is widely used by the framework, and add additional comments for clarification. Benchmark results of the old and new implementations (using `slice` makes it slightly faster) are as follows: ``` stringify (old version) x 117,945,419 ops/sec ±5.25% (55 runs sampled) stringify (new version) x 136,692,820 ops/sec ±4.82% (56 runs sampled) ``` PR Close #59745
1 parent 236a623 commit 354b1fa

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

packages/core/src/util/stringify.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,26 @@ export function stringify(token: any): string {
1212
}
1313

1414
if (Array.isArray(token)) {
15-
return '[' + token.map(stringify).join(', ') + ']';
15+
return `[${token.map(stringify).join(', ')}]`;
1616
}
1717

1818
if (token == null) {
1919
return '' + token;
2020
}
2121

22-
if (token.overriddenName) {
23-
return `${token.overriddenName}`;
22+
const name = token.overriddenName || token.name;
23+
if (name) {
24+
return `${name}`;
2425
}
2526

26-
if (token.name) {
27-
return `${token.name}`;
28-
}
29-
30-
const res = token.toString();
27+
const result = token.toString();
3128

32-
if (res == null) {
33-
return '' + res;
29+
if (result == null) {
30+
return '' + result;
3431
}
3532

36-
const newLineIndex = res.indexOf('\n');
37-
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
33+
const newLineIndex = result.indexOf('\n');
34+
return newLineIndex >= 0 ? result.slice(0, newLineIndex) : result;
3835
}
3936

4037
/**

0 commit comments

Comments
 (0)