From 9957999fb5d9db202e912d1fb07cb98c8f31556d Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 6 Jun 2024 06:15:54 +0000 Subject: [PATCH] Store node children WeakMaps in vars and bind its methods as locals. --- src/compiler/factory/nodeChildren.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/compiler/factory/nodeChildren.ts b/src/compiler/factory/nodeChildren.ts index 79e8563f6f5..fa77de189fc 100644 --- a/src/compiler/factory/nodeChildren.ts +++ b/src/compiler/factory/nodeChildren.ts @@ -4,18 +4,25 @@ import { Node, } from "../_namespaces/ts.js"; -const nodeChildren = new WeakMap(); +// 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(); +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; }