Skip to content

Commit cb988ad

Browse files
author
Anatoly Ostrovsky
committed
Noderef optimizations
1 parent 64ec8a0 commit cb988ad

File tree

4 files changed

+23
-58
lines changed

4 files changed

+23
-58
lines changed

@types/shared/noderef.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ export class NodeRef {
99
* @throws {Error} If the argument is invalid or cannot be wrapped properly.
1010
*/
1111
constructor(element: Node | Element | string | NodeList | Node[]);
12-
initial: string | Node | NodeList | Node[];
1312
/** @private @type {Node | ChildNode | null} */
1413
private _node;
1514
/** @private @type {Element | undefined} */
1615
private _element;
1716
/** @private @type {Array<Node>} a stable list on nodes */
1817
private _nodes;
1918
/** @type {boolean} */
20-
linked: boolean;
21-
/** @type {boolean} */
22-
isList: boolean;
19+
_isList: boolean;
2320
/** @param {Element} el */
2421
set element(el: Element);
2522
/** @returns {Element} */

src/core/compile/compile.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,6 @@ export class CompileProvider {
760760
nodeRef = compositeLinkFn = null;
761761
}
762762

763-
$linkNode.linked = true;
764-
765763
return $linkNode.getAll();
766764
}
767765
}
@@ -898,15 +896,15 @@ export class CompileProvider {
898896

899897
if (nodeLinkFnFound) {
900898
// create a stable copy of the nodeList, only copying elements with linkFns
901-
const stableLength = nodeRef.isList ? nodeRef.nodes.length : 1;
899+
const stableLength = nodeRef._isList ? nodeRef.nodes.length : 1;
902900

903901
stableNodeList = new Array(stableLength);
904902
// create a sparse array by only copying the elements which have a linkFn
905903
linkFnsList.forEach((val) => {
906904
const idx = val.index;
907905

908906
if (idx === 0) {
909-
stableNodeList[idx] = nodeRef.isList
907+
stableNodeList[idx] = nodeRef._isList
910908
? nodeRef.nodes[idx]
911909
: nodeRef.node;
912910
} else {
@@ -916,7 +914,7 @@ export class CompileProvider {
916914
}
917915
});
918916
} else {
919-
if (nodeRef.isList) {
917+
if (nodeRef._isList) {
920918
nodeRef.nodes.forEach((elem) => stableNodeList.push(elem));
921919
} else {
922920
stableNodeList.push(nodeRef.node);

src/shared/noderef.js

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertArg, isArray, isString } from "./utils.js";
1+
import { isArray, isString } from "./utils.js";
22
import { createElementFromHTML, NodeType } from "./dom.js";
33

44
/**
@@ -12,9 +12,6 @@ export class NodeRef {
1212
* @throws {Error} If the argument is invalid or cannot be wrapped properly.
1313
*/
1414
constructor(element) {
15-
assertArg(element, "element");
16-
this.initial = null;
17-
1815
/** @private @type {Node | ChildNode | null} */
1916
this._node = null;
2017

@@ -25,14 +22,10 @@ export class NodeRef {
2522
this._nodes = undefined;
2623

2724
/** @type {boolean} */
28-
this.linked = false;
29-
30-
/** @type {boolean} */
31-
this.isList = false;
25+
this._isList = false;
3226

3327
// Handle HTML string
3428
if (isString(element)) {
35-
this.initial = element;
3629
const res = createElementFromHTML(/** @type {string} */ (element));
3730

3831
switch (true) {
@@ -47,37 +40,29 @@ export class NodeRef {
4740

4841
// Handle NodeList
4942
else if (element instanceof NodeList) {
50-
this.initial = Array.from(element).map((elem) => elem.cloneNode(true));
51-
5243
if (element.length === 1) {
5344
this.node = element[0];
5445
} else {
5546
this._nodes = Array.from(element);
56-
this.isList = true;
47+
this._isList = true;
5748
}
5849
}
5950

6051
// Handle single Element
6152
else if (element instanceof Element) {
62-
this.initial = element.cloneNode(true);
6353
this.element = /** @type {Element} */ element;
6454
}
6555

6656
// Handle single Node
6757
else if (element instanceof Node) {
68-
this.initial = element.cloneNode(true);
6958
this._node = element;
7059
}
7160

7261
// Handle array of elements
7362
else if (isArray(element)) {
7463
if (element.length === 1) {
7564
this.node = /** @type {Node} */ (element[0]);
76-
this.initial = this.node.cloneNode(true);
7765
} else {
78-
this.initial = Array.from(/** @type {Node[]} */ (element)).map((node) =>
79-
node.cloneNode(true),
80-
);
8166
this.nodes = /** @type {Node[]} */ (element);
8267
}
8368
} else {
@@ -87,29 +72,23 @@ export class NodeRef {
8772

8873
/** @returns {Element} */
8974
get element() {
90-
assertArg(this._element, "element");
91-
9275
return this._element;
9376
}
9477

9578
/** @param {Element} el */
9679
set element(el) {
97-
assertArg(el instanceof Element, "element");
9880
this._element = el;
9981
this._nodes = undefined;
100-
this.isList = false;
82+
this._isList = false;
10183
}
10284

10385
/** @returns {Node | ChildNode} */
10486
get node() {
105-
assertArg(this._node || this._element, "node");
106-
10787
return this._node || this._element;
10888
}
10989

11090
/** @param {Node | ChildNode} node */
11191
set node(node) {
112-
assertArg(node instanceof Node, "node");
11392
this._node = node;
11493

11594
if (node.nodeType === NodeType._ELEMENT_NODE) {
@@ -121,18 +100,12 @@ export class NodeRef {
121100

122101
/** @param {Array<Node>} nodes */
123102
set nodes(nodes) {
124-
assertArg(
125-
isArray(nodes) && nodes.every((node) => node instanceof Node),
126-
"nodes",
127-
);
128103
this._nodes = nodes;
129-
this.isList = true;
104+
this._isList = true;
130105
}
131106

132107
/** @returns {Array<Node>} */
133108
get nodes() {
134-
assertArg(this._nodes, "nodes");
135-
136109
return this._nodes;
137110
}
138111

@@ -151,18 +124,18 @@ export class NodeRef {
151124

152125
/** @returns {Element | Node | ChildNode | NodeList | Node[]} */
153126
get dom() {
154-
if (this.isList) return this.nodelist;
127+
if (this._isList) return this.nodelist;
155128
else return this.node;
156129
}
157130

158131
/** @returns {number} */
159132
get size() {
160-
return this.isList ? this._nodes.length : 1;
133+
return this._isList ? this._nodes.length : 1;
161134
}
162135

163136
/** @returns {Element | Node | ChildNode} */
164137
getAny() {
165-
if (this.isList) {
138+
if (this._isList) {
166139
return this._nodes[0];
167140
} else {
168141
return this._element || this._node;
@@ -171,7 +144,7 @@ export class NodeRef {
171144

172145
/** @returns {Element | Array<Node> | Node | ChildNode} */
173146
getAll() {
174-
if (this.isList) {
147+
if (this._isList) {
175148
return this._nodes;
176149
} else {
177150
return this._element || this._node;
@@ -180,7 +153,7 @@ export class NodeRef {
180153

181154
/** @returns {Array<Element> | Array<Node>} */
182155
collection() {
183-
if (this.isList) {
156+
if (this._isList) {
184157
return Array.from(this._nodes);
185158
} else {
186159
return [this._element || this._node];
@@ -192,7 +165,7 @@ export class NodeRef {
192165
* @returns {Element | Node | ChildNode}
193166
*/
194167
getIndex(index) {
195-
if (this.isList) {
168+
if (this._isList) {
196169
return this._nodes[index];
197170
} else {
198171
return this.node;
@@ -204,10 +177,7 @@ export class NodeRef {
204177
* @param {Element | Node | ChildNode} node
205178
*/
206179
setIndex(index, node) {
207-
assertArg(index !== null, "index");
208-
assertArg(node, "node");
209-
210-
if (this.isList) {
180+
if (this._isList) {
211181
this._nodes[index] = node;
212182
} else {
213183
this.node = node;
@@ -218,7 +188,7 @@ export class NodeRef {
218188
* @returns {NodeRef}
219189
*/
220190
clone() {
221-
const cloned = this.isList
191+
const cloned = this._isList
222192
? this.nodes.map((el) => el.cloneNode(true))
223193
: this.node.cloneNode(true);
224194

src/shared/noderef.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("NodeRef", () => {
1919
expect(ref.node).toBe(div);
2020
expect(ref.element).toBe(div);
2121
expect(ref.size).toBe(1);
22-
expect(ref.isList).toBeFalse();
22+
expect(ref._isList).toBeFalse();
2323
expect(ref.isElement()).toBeTrue();
2424
});
2525

@@ -32,14 +32,14 @@ describe("NodeRef", () => {
3232
it("wraps an array of Nodes", () => {
3333
const ref = new NodeRef([div, span]);
3434
expect(ref.nodes).toEqual([div, span]);
35-
expect(ref.isList).toBeTrue();
35+
expect(ref._isList).toBeTrue();
3636
expect(ref.size).toBe(2);
3737
});
3838

3939
it("wraps an array with one Node", () => {
4040
const ref = new NodeRef([div]);
4141
expect(ref.node).toBe(div);
42-
expect(ref.isList).toBeFalse();
42+
expect(ref._isList).toBeFalse();
4343
expect(ref.size).toBe(1);
4444
});
4545

@@ -48,7 +48,7 @@ describe("NodeRef", () => {
4848
parent.append(div, span);
4949
const ref = new NodeRef(parent.childNodes);
5050
expect(ref.nodes).toEqual([div, span]);
51-
expect(ref.isList).toBeTrue();
51+
expect(ref._isList).toBeTrue();
5252
});
5353

5454
it("wraps an HTML string", () => {
@@ -86,7 +86,7 @@ describe("NodeRef", () => {
8686
const ref = new NodeRef(div);
8787
ref.nodes = [div, span];
8888
expect(ref.nodes).toEqual([div, span]);
89-
expect(ref.isList).toBeTrue();
89+
expect(ref._isList).toBeTrue();
9090
});
9191

9292
it("returns correct element and node", () => {
@@ -173,7 +173,7 @@ describe("NodeRef", () => {
173173
const frag = document.createDocumentFragment();
174174
frag.append(div, span);
175175
const ref = new NodeRef(frag.childNodes);
176-
expect(ref.isList).toBeTrue();
176+
expect(ref._isList).toBeTrue();
177177
expect(ref.nodes.length).toBe(2);
178178
});
179179

0 commit comments

Comments
 (0)