【发布时间】:2019-07-03 15:14:25
【问题描述】:
在 TypeScript 编译器中,src/compiler/parser.ts 包含以下内容,其中 identifiers 是字符串到字符串的 Map:
function internIdentifier(text: string): string {
let identifier = identifiers.get(text);
if (identifier === undefined) {
identifiers.set(text, identifier = text);
}
return identifier;
}
这与字符串的识别函数具有相同的行为:
const id = (text: string) => text
我认为它是为了性能。这如何提高性能?我问是因为:
- 我认为 JS VM 已经实习过字符串(但还没有找到证据)
- 代码似乎没有节省字符串的创建时间。必须创建一个字符串 (
text) 才能在地图中查找相同的字符串。
【问题讨论】:
标签: typescript compiler-construction compiler-optimization