Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ui-node-construction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': patch
---

Improves the speed of building DOM nodes with `createElement` by about three times
7 changes: 5 additions & 2 deletions packages/ui/src/ascii.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// The native case maps agree with the ASCII ones on ASCII-only strings and are much faster.
const NON_ASCII = /[^\0-\x7f]/;

export function asciiLowercase(s: string): string {
return s.replace(/[A-Z]/g, (c) => c.toLowerCase());
return NON_ASCII.test(s) ? s.replace(/[A-Z]/g, (c) => c.toLowerCase()) : s.toLowerCase();
}

export function asciiUppercase(s: string): string {
return s.replace(/[a-z]/g, (c) => c.toUpperCase());
return NON_ASCII.test(s) ? s.replace(/[a-z]/g, (c) => c.toUpperCase()) : s.toUpperCase();
}
Comment thread
natemoo-re marked this conversation as resolved.
Resolved
42 changes: 30 additions & 12 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { findElement, findElements, querySelector } from './css-select.ts';
import { asciiUppercase, asciiLowercase } from './ascii.ts';
import { NamedNodeMap } from './named-node-map.ts';
import { exposeAttributes, NamedNodeMap } from './named-node-map.ts';
import { HTMLCollection } from './html-collection.ts';
import { StyleDeclaration } from './style.ts';
import {
Expand All @@ -23,6 +23,17 @@ import {

const HTML_NS = 'http://www.w3.org/1999/xhtml';

const NODE_TYPE_NAMES = [
'ELEMENT_NODE',
'ATTRIBUTE_NODE',
'TEXT_NODE',
'PROCESSING_INSTRUCTION_NODE',
'COMMENT_NODE',
'DOCUMENT_NODE',
'DOCUMENT_TYPE_NODE',
'DOCUMENT_FRAGMENT_NODE',
] as const;

export abstract class Node implements EventTarget {
static readonly ELEMENT_NODE = 1;
static readonly ATTRIBUTE_NODE = 2;
Expand All @@ -33,14 +44,14 @@ export abstract class Node implements EventTarget {
static readonly DOCUMENT_TYPE_NODE = 10;
static readonly DOCUMENT_FRAGMENT_NODE = 11;

readonly ELEMENT_NODE = Node.ELEMENT_NODE;
readonly ATTRIBUTE_NODE = Node.ATTRIBUTE_NODE;
readonly TEXT_NODE = Node.TEXT_NODE;
readonly PROCESSING_INSTRUCTION_NODE = Node.PROCESSING_INSTRUCTION_NODE;
readonly COMMENT_NODE = Node.COMMENT_NODE;
readonly DOCUMENT_NODE = Node.DOCUMENT_NODE;
readonly DOCUMENT_TYPE_NODE = Node.DOCUMENT_TYPE_NODE;
readonly DOCUMENT_FRAGMENT_NODE = Node.DOCUMENT_FRAGMENT_NODE;
declare readonly ELEMENT_NODE: 1;
declare readonly ATTRIBUTE_NODE: 2;
declare readonly TEXT_NODE: 3;
declare readonly PROCESSING_INSTRUCTION_NODE: 7;
declare readonly COMMENT_NODE: 8;
declare readonly DOCUMENT_NODE: 9;
declare readonly DOCUMENT_TYPE_NODE: 10;
declare readonly DOCUMENT_FRAGMENT_NODE: 11;

readonly #childNodes: Node[] = [];
readonly ownerDocument: Document | null;
Expand All @@ -54,6 +65,13 @@ export abstract class Node implements EventTarget {
this.ownerDocument = owner;
}

static {
// Web IDL constants live on the interface object and its prototype, not on each instance.
for (const name of NODE_TYPE_NAMES) {
Object.defineProperty(Node.prototype, name, { value: Node[name], enumerable: true });
}
}

protected notify(): void {
this.ownerDocument?.notify();
}
Expand Down Expand Up @@ -207,8 +225,8 @@ export abstract class Node implements EventTarget {
node.parentNode.removeChild(node);
}
}
const anchorIndex = anchor ? this.childNodes.indexOf(anchor) : this.childNodes.length;
this.#childNodes.splice(anchorIndex, 0, node);
if (anchor) this.#childNodes.splice(this.#childNodes.indexOf(anchor), 0, node);
else this.#childNodes.push(node);

node.#parentNode = this;
this.notify();
Expand Down Expand Up @@ -536,7 +554,7 @@ export class Element extends Node {
}

get attributes(): NamedNodeMap {
return this.#attributes;
return exposeAttributes(this.#attributes);
}

getAttribute(name: string): string | null {
Expand Down
25 changes: 23 additions & 2 deletions packages/ui/src/named-node-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type AttributeFactory = (localName: string, namespace: AttributeNamespace) => At
/** Called with `null` when the attribute is removed. */
export type AttributeChanged = (attr: Attr, value: string | null) => void;

/**
* Marks the map as reachable from script, as Element.attributes hands it out. Only then does it keep its indexed and
* named properties, so maps nothing has read cost nothing to change.
*/
export let exposeAttributes: (map: NamedNodeMap) => NamedNodeMap;
Comment thread
natemoo-re marked this conversation as resolved.
Resolved

/**
* Holds Element attributes: https://dom.spec.whatwg.org/#interface-namednodemap
*
Expand All @@ -22,6 +28,17 @@ export class NamedNodeMap {
readonly #attributeChanged: AttributeChanged;
readonly #attributes: Attr[] = [];
#properties: string[] = [];
#exposed = false;

static {
exposeAttributes = (map) => {
if (!map.#exposed) {
map.#exposed = true;
map.#reflect();
}
return map;
};
}

constructor({
ownerElement,
Expand Down Expand Up @@ -179,7 +196,12 @@ export class NamedNodeMap {
}

#changed(): void {
// Rebuild the indexed and named views when the ordered list changes.
if (this.#exposed) this.#reflect();
this.#ownerElement.ownerDocument.notify();
}

/** Rebuilds the indexed and named views from the ordered list. */
#reflect(): void {
for (const key of this.#properties) Reflect.deleteProperty(this, key);
this.#properties = [];
for (const [index, attr] of this.#attributes.entries()) {
Expand All @@ -195,6 +217,5 @@ export class NamedNodeMap {
Object.defineProperty(this, name, { value: attr, configurable: true });
this.#properties.push(name);
}
this.#ownerElement.ownerDocument.notify();
}
}
Loading