in the TypeScript compiler, src/compiler/parser.ts contains the following, where identifiers
is a Map
of strings to strings:
function internIdentifier(text: string): string {
let identifier = identifiers.get(text);
if (identifier === undefined) {
identifiers.set(text, identifier = text);
}
return identifier;
}
This has the same behavior as the identify function for strings:
const id = (text: string) => text
I assume it's there for performance. How could this improve performance? I'm asking because:
- I think JS VMs already intern strings (but haven't found evidence yet)
- the code doesn't seem to save on string creation. One must create a string (
text
) in order to look up the same string in the map.