chore: use WeakMap and null for DepthCalculator (#9892)

This commit is contained in:
jrandolf 2023-03-21 14:09:50 +01:00 committed by GitHub
parent 1f76cdda12
commit 323db305f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -172,10 +172,10 @@ class PQueryEngine {
}
class DepthCalculator {
#cache = new Map<Node, number[]>();
#cache = new WeakMap<Node, number[]>();
calculate(node: Node, depth: number[] = []): number[] {
if (node instanceof Document) {
calculate(node: Node | null, depth: number[] = []): number[] {
if (node === null) {
return depth;
}
if (node instanceof ShadowRoot) {
@ -196,7 +196,7 @@ class DepthCalculator {
++index;
}
const value = this.calculate(node.parentNode as Node, [index]);
const value = this.calculate(node.parentNode, [index]);
this.#cache.set(node, value);
return [...value, ...depth];
}