-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Minor performance issue when using HTMLElementTagNameMapΒ #63347
Description
π Search Terms
HTMLElementTagNameMap
performance
π Version & Regression Information
- This changed between versions 5.9.3 and 6.0.2
β― Playground Link
π» Code
function mapElement<K extends keyof HTMLElementTagNameMap>(element: K): HTMLElementTagNameMap[K]
function mapElement<K extends keyof SVGElementTagNameMap >(element: K): SVGElementTagNameMap[K]
function mapElement(element: any): HTMLElement {
return document.createElement(element);
}
action(mapElement('div'));
function action(ele: HTMLElement) {
} π Actual behavior
The type check for this specific overload is slower in TypeScript 6.0, but still bearable (under 50ms on a modern PC). From the flame graph, it seems like the hot path is
This also seems to happen in TypeScript 7, but I can't reliably reproduce it. It doesn't happen on my Windows machine, but on my Linux machine.
Although this seems minor, it is a bit odd that TypeScript needed to create a union of all the HTML elements, comparing all the properties to choose an overload. If this doesn't match the merge criteria, maybe it can still be improved in TypeScript 7? Or maybe there is another way to type this function that won't trigger this?
π Expected behavior
performance improvement.
Additional information about the issue
For context, this issue was found in our performance smoke test in https://github.com/sveltejs/language-tools. It consistently failed with TypeScript 6. The virtual code for the Svelte file includes a svelteHTML.mapElementTag('div') function call. The function is similar to the reproduction, just that it uses the deprecated ElementTagNameMap.