Skip to content

Commit d38d1b9

Browse files
committed
Further improve performance of Document creation.
This speeds up the create test by about **25-30%** by avoiding the call to `util.assign()` in the `createDocument()` function. Instead of that `Document#document` is now an accessor on the prototype, `defaultView` is an instance of a new `DefaultView` class, whose instances have links to the `document`, and finally the interesting constructors that are exposed via `Document` instances are all installed on the `Document` and `DefaultView` prototypes instead of the instances.
1 parent f58a931 commit d38d1b9

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/undom.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,32 @@ class Element extends Node {
151151
}
152152

153153

154+
class Event {
155+
constructor(type, opts) {
156+
this.type = type;
157+
this.bubbles = !!(opts && opts.bubbles);
158+
this.cancelable = !!(opts && opts.cancelable);
159+
}
160+
stopPropagation() {
161+
this._stop = true;
162+
}
163+
stopImmediatePropagation() {
164+
this._end = this._stop = true;
165+
}
166+
preventDefault() {
167+
this.defaultPrevented = true;
168+
}
169+
}
170+
171+
154172
class Document extends Element {
155173
constructor() {
156174
super(9, '#document'); // DOCUMENT_NODE
175+
this.defaultView = new DefaultView(this);
176+
}
177+
178+
get document() {
179+
return this;
157180
}
158181

159182
createElement(type) {
@@ -166,37 +189,28 @@ class Document extends Element {
166189
return element;
167190
}
168191

169-
170192
createTextNode(text) {
171193
return new Text(text);
172194
}
173195
}
174196

197+
assign(Document.prototype, { Document, Node, Text, Element, SVGElement: Element, Event });
175198

176-
class Event {
177-
constructor(type, opts) {
178-
this.type = type;
179-
this.bubbles = !!(opts && opts.bubbles);
180-
this.cancelable = !!(opts && opts.cancelable);
181-
}
182-
stopPropagation() {
183-
this._stop = true;
184-
}
185-
stopImmediatePropagation() {
186-
this._end = this._stop = true;
187-
}
188-
preventDefault() {
189-
this.defaultPrevented = true;
199+
200+
class DefaultView {
201+
constructor(document) {
202+
this.document = document;
190203
}
191204
}
192205

206+
assign(DefaultView.prototype, { Document, Node, Text, Element, SVGElement: Element, Event });
207+
193208

194209
/** Create a minimally viable DOM Document
195210
* @returns {Document} document
196211
*/
197212
export default function createDocument() {
198213
let document = new Document();
199-
assign(document, document.defaultView = { document, Document, Node, Text, Element, SVGElement: Element, Event });
200214
document.appendChild(
201215
document.documentElement = document.createElement('html')
202216
);

0 commit comments

Comments
 (0)