Commit 074cf4eb authored by David Sveningsson's avatar David Sveningsson
Browse files

Merge branch 'perf/cache-generate-selector' into 'master'

Improve performance with caching and ID counting

See merge request !1313
parents bc3b3477 cc938fbd
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -477,6 +477,8 @@ export class HtmlElement extends DOMNode {
    // @internal (undocumented)
    readonly _adapter: HtmlElementLike;
    get annotatedName(): string;
    // (undocumented)
    append(node: DOMNode): void;
    appendText(text: string | DynamicValue, location: Location_2): void;
    get ariaLabelledby(): string[] | DynamicValue | null;
    // (undocumented)
@@ -511,6 +513,8 @@ export class HtmlElement extends DOMNode {
    // (undocumented)
    hasAttribute(key: string): boolean;
    get id(): string | null;
    // (undocumented)
    insertBefore(node: DOMNode, reference: DOMNode | null): void;
    is(tagName: string): boolean;
    get lastElementChild(): HtmlElement | null;
    loadMeta(meta: MetaElement): void;
@@ -527,6 +531,8 @@ export class HtmlElement extends DOMNode {
    querySelector(selector: string): HtmlElement | null;
    // (undocumented)
    querySelectorAll(selector: string): HtmlElement[];
    // (undocumented)
    removeChild<T extends DOMNode>(node: T): T;
    get role(): string | DynamicValue | null;
    // @internal (undocumented)
    static rootNode(location: Location_2): HtmlElement;
+6 −0
Original line number Diff line number Diff line
@@ -568,6 +568,8 @@ export class HtmlElement extends DOMNode {
    // @internal (undocumented)
    readonly _adapter: HtmlElementLike;
    get annotatedName(): string;
    // (undocumented)
    append(node: DOMNode): void;
    appendText(text: string | DynamicValue, location: Location_2): void;
    get ariaLabelledby(): string[] | DynamicValue | null;
    // (undocumented)
@@ -602,6 +604,8 @@ export class HtmlElement extends DOMNode {
    // (undocumented)
    hasAttribute(key: string): boolean;
    get id(): string | null;
    // (undocumented)
    insertBefore(node: DOMNode, reference: DOMNode | null): void;
    is(tagName: string): boolean;
    get lastElementChild(): HtmlElement | null;
    loadMeta(meta: MetaElement): void;
@@ -618,6 +622,8 @@ export class HtmlElement extends DOMNode {
    querySelector(selector: string): HtmlElement | null;
    // (undocumented)
    querySelectorAll(selector: string): HtmlElement[];
    // (undocumented)
    removeChild<T extends DOMNode>(node: T): T;
    get role(): string | DynamicValue | null;
    // @internal (undocumented)
    static rootNode(location: Location_2): HtmlElement;
+22 −1
Original line number Diff line number Diff line
@@ -12,11 +12,13 @@ import { NodeType } from "./nodetype";
import { Selector, generateIdSelector } from "./selector";
import { TextNode } from "./text";

const CHILD_ELEMENTS = Symbol("childElements");
const ROLE = Symbol("role");
const TABINDEX = Symbol("tabindex");

declare module "./cache" {
	export interface DOMNodeCache {
		[CHILD_ELEMENTS]: HtmlElement[];
		[ROLE]: string;
		[TABINDEX]: number | null;
	}
@@ -249,7 +251,11 @@ export class HtmlElement extends DOMNode {
	 * Similar to childNodes but only elements.
	 */
	public get childElements(): HtmlElement[] {
		return this.childNodes.filter(isElementNode);
		const cached = this.cacheGet(CHILD_ELEMENTS);
		if (cached !== undefined) {
			return cached;
		}
		return this.cacheSet(CHILD_ELEMENTS, this.childNodes.filter(isElementNode));
	}

	/**
@@ -734,12 +740,27 @@ export class HtmlElement extends DOMNode {
		return visit(this);
	}

	public override append(node: DOMNode): void {
		super.append(node);
		this.cacheRemove(CHILD_ELEMENTS);
	}

	public override insertBefore(node: DOMNode, reference: DOMNode | null): void {
		super.insertBefore(node, reference);
		this.cacheRemove(CHILD_ELEMENTS);
	}

	public override removeChild<T extends DOMNode>(node: T): T {
		return super.removeChild(node);
	}

	/**
	 * @internal
	 */
	public override _setParent(node: DOMNode | null): DOMNode | null {
		const oldParent = this._parent;
		this._parent = node instanceof HtmlElement ? node : null;
		oldParent?.cacheRemove(CHILD_ELEMENTS);
		return oldParent;
	}
}