mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
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
This commit is contained in:
parent
967488247e
commit
cf3a5073ec
1 changed files with 10 additions and 13 deletions
|
|
@ -12,29 +12,26 @@ export function stringify(token: any): string {
|
|||
}
|
||||
|
||||
if (Array.isArray(token)) {
|
||||
return '[' + token.map(stringify).join(', ') + ']';
|
||||
return `[${token.map(stringify).join(', ')}]`;
|
||||
}
|
||||
|
||||
if (token == null) {
|
||||
return '' + token;
|
||||
}
|
||||
|
||||
if (token.overriddenName) {
|
||||
return `${token.overriddenName}`;
|
||||
const name = token.overriddenName || token.name;
|
||||
if (name) {
|
||||
return `${name}`;
|
||||
}
|
||||
|
||||
if (token.name) {
|
||||
return `${token.name}`;
|
||||
const result = token.toString();
|
||||
|
||||
if (result == null) {
|
||||
return '' + result;
|
||||
}
|
||||
|
||||
const res = token.toString();
|
||||
|
||||
if (res == null) {
|
||||
return '' + res;
|
||||
}
|
||||
|
||||
const newLineIndex = res.indexOf('\n');
|
||||
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
||||
const newLineIndex = result.indexOf('\n');
|
||||
return newLineIndex >= 0 ? result.slice(0, newLineIndex) : result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue