Store node children WeakMaps in vars and bind its methods as locals.

This commit is contained in:
Daniel Rosenwasser
2024-06-06 06:15:54 +00:00
parent c325b103d0
commit 9957999fb5

View File

@@ -4,18 +4,25 @@ import {
Node,
} from "../_namespaces/ts.js";
const nodeChildren = new WeakMap<Node, readonly Node[] | undefined>();
// Why var? It avoids TDZ checks in the runtime which can be costly.
// See: https://github.com/microsoft/TypeScript/issues/52924
/* eslint-disable no-var */
var nodeChildren = new WeakMap<Node, readonly Node[] | undefined>();
var nodeChildrenGet = nodeChildren.get.bind(nodeChildren);
var nodeChildrenSet = nodeChildren.set.bind(nodeChildren);
/* eslint-enable no-var */
/** @internal */
export function getNodeChildren(node: Node): readonly Node[] | undefined {
if (!isNodeKind(node.kind)) return emptyArray;
return nodeChildren.get(node);
return nodeChildrenGet(node);
}
/** @internal */
export function setNodeChildren(node: Node, children: readonly Node[]): readonly Node[] {
nodeChildren.set(node, children);
nodeChildrenSet(node, children);
return children;
}