Skip to content

Commit 1c22657

Browse files
committed
Removed unnecessary overhead carried by majinbuu logic.
1 parent 5f29a86 commit 1c22657

11 files changed

Lines changed: 1020 additions & 1033 deletions

File tree

cjs/classes/Aura.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,25 @@ function Aura(node, childNodes) {
1111
this.childNodes = childNodes;
1212
}
1313

14+
Aura.prototype.empty = function empty(value) {
15+
const node = this.node;
16+
const childNodes = this.childNodes;
17+
const pn = node.parentNode;
18+
let length = childNodes.length;
19+
if (length) {
20+
const remove = childNodes.splice(0, length);
21+
while (length--) pn.removeChild(asNode(remove[length]));
22+
}
23+
if (value) {
24+
childNodes.push(value);
25+
pn.insertBefore(asNode(value), node);
26+
}
27+
};
28+
1429
Aura.prototype.become = function become(virtual) {
30+
const node = this.node;
1531
const live = this.childNodes;
32+
const pn = node.parentNode;
1633
const vlength = virtual.length;
1734
let llength = live.length;
1835
let l = 0;
@@ -22,53 +39,45 @@ Aura.prototype.become = function become(virtual) {
2239
const vv = virtual[v];
2340
const status = lv === vv ? 0 : (live.indexOf(vv) < 0 ? 1 : -1);
2441
if (status < 0) {
25-
this.splice(l, 1);
42+
live.splice(l, 1);
43+
pn.removeChild(asNode(lv));
2644
llength--;
2745
} else if (0 < status) {
28-
this.splice(l++, 0, virtual[v++]);
46+
live.splice(l++, 0, vv);
47+
pn.insertBefore(asNode(vv), l < llength ? asNode(live[l]) : node);
2948
llength++;
49+
v++;
3050
} else {
3151
l++;
3252
v++;
3353
}
3454
}
3555
if (l < llength) {
36-
this.splice(l, llength - l);
56+
const remove = live.splice(l, llength - l);
57+
l = remove.length;
58+
while (l--) pn.removeChild(asNode(remove[l]));
3759
}
3860
if (v < vlength) {
39-
this.splice.apply(this, [llength, 0].concat(virtual.slice(v)));
40-
}
41-
};
42-
43-
// the splice is in charge of removing or adding nodes
44-
Aura.prototype.splice = function splice(start, end) {
45-
const values = new Map;
46-
const ph = this.node;
47-
const cn = this.childNodes;
48-
const target = get(values, cn[start + (end || 0)] || ph);
49-
const pn = ph.parentNode;
50-
const result = cn.splice.apply(cn, arguments);
51-
const reLength = result.length;
52-
for (let i = 0; i < reLength; i++) {
53-
pn.removeChild(get(values, result[i]));
54-
}
55-
const arLength = arguments.length;
56-
if (3 === arLength) {
57-
pn.insertBefore(get(values, arguments[2]), target);
58-
} else if (2 < arLength) {
59-
const tmp = fragment(pn);
60-
for (let i = 2; i < arLength; i++) {
61-
tmp.appendChild(get(values, arguments[i]));
61+
const append = virtual.slice(v);
62+
l = 0;
63+
llength = append.length;
64+
if (llength === 1) {
65+
pn.insertBefore(asNode(append[l]), node);
66+
} else {
67+
const tmp = fragment(pn);
68+
while (l < llength)
69+
tmp.appendChild(asNode(append[l++]));
70+
pn.insertBefore(tmp, node);
6271
}
63-
pn.insertBefore(tmp, target);
72+
live.push.apply(live, append);
6473
}
65-
return result;
6674
};
6775

6876
// an item could be an hyperHTML.Component and, in such case,
6977
// it should be rendered as node
7078
const asNode = node => node instanceof Component ? node.render() : node;
7179

80+
/* TODO: benchmark this is needed at all
7281
// instead of checking instanceof each time and render potentially twice
7382
// use a map to retrieve nodes from a generic item
7483
const get = (map, node) => map.get(node) || set(map, node);
@@ -77,5 +86,6 @@ const set = (map, node) => {
7786
map.set(node, value);
7887
return value;
7988
};
89+
*/
8090

8191
Object.defineProperty(exports, '__esModule', {value: true}).default = Aura;

cjs/objects/Updates.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,31 +220,22 @@ const isSpecial = (node, name) => !(OWNER_SVG_ELEMENT in node) && name in node;
220220
// update the node with the resulting list of content
221221
const setAnyContent = (node, childNodes) => {
222222
const aura = new Aura(node, childNodes);
223+
let fastPath = false;
223224
let oldValue;
224225
const anyContent = value => {
225226
switch (typeof value) {
226227
case 'string':
227228
case 'number':
228229
case 'boolean':
229-
let length = childNodes.length;
230-
if (
231-
length === 1 &&
232-
childNodes[0].nodeType === TEXT_NODE
233-
) {
230+
if (fastPath) {
234231
if (oldValue !== value) {
235232
oldValue = value;
236233
childNodes[0].textContent = value;
237234
}
238235
} else {
236+
fastPath = true;
239237
oldValue = value;
240-
if (length) {
241-
aura.splice(0, length, text(node, value));
242-
} else {
243-
node.parentNode.insertBefore(
244-
(childNodes[0] = text(node, value)),
245-
node
246-
);
247-
}
238+
aura.empty(text(node, value));
248239
}
249240
break;
250241
case 'object':
@@ -258,7 +249,7 @@ const setAnyContent = (node, childNodes) => {
258249
oldValue = value;
259250
if (isArray(value)) {
260251
if (value.length === 0) {
261-
aura.splice(0);
252+
aura.empty();
262253
} else {
263254
switch (typeof value[0]) {
264255
case 'string':
@@ -280,7 +271,7 @@ const setAnyContent = (node, childNodes) => {
280271
}
281272
}
282273
} else if (value instanceof Component) {
283-
aura.become([value]);
274+
aura.empty(value);
284275
} else if (isNode_ish(value)) {
285276
aura.become(value.nodeType === DOCUMENT_FRAGMENT_NODE ?
286277
slice.call(value.childNodes) :
@@ -294,7 +285,7 @@ const setAnyContent = (node, childNodes) => {
294285
} else if ('any' in value) {
295286
anyContent(value.any);
296287
} else if ('html' in value) {
297-
aura.splice(0);
288+
aura.empty();
298289
const fragment = createFragment(node, [].concat(value.html).join(''));
299290
childNodes.push.apply(childNodes, fragment.childNodes);
300291
node.parentNode.insertBefore(fragment, node);

coverage/coverage.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)