From 323db305f03912187e35b2f198f61226af8cc9a1 Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Tue, 21 Mar 2023 14:09:50 +0100 Subject: [PATCH] chore: use WeakMap and null for DepthCalculator (#9892) --- packages/puppeteer-core/src/injected/PQuerySelector.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/puppeteer-core/src/injected/PQuerySelector.ts b/packages/puppeteer-core/src/injected/PQuerySelector.ts index c136cda772d..3bd75c4e9b1 100644 --- a/packages/puppeteer-core/src/injected/PQuerySelector.ts +++ b/packages/puppeteer-core/src/injected/PQuerySelector.ts @@ -172,10 +172,10 @@ class PQueryEngine { } class DepthCalculator { - #cache = new Map(); + #cache = new WeakMap(); - 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]; }