Skip to content

Commit c40b54c

Browse files
committed
v4.0.6 contd, minor optimization changes
1 parent 7356cf2 commit c40b54c

4 files changed

Lines changed: 75 additions & 47 deletions

File tree

build/modelview.bundle.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/modelview.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* ModelView.js
44
* @version: 4.0.6
5-
* @built on 2022-03-07 20:29:32
5+
* @built on 2022-03-08 10:08:22
66
*
77
* A simple, light-weight, versatile and fast isomorphic MVVM JavaScript framework (Browser and Server)
88
* https://github.com/foo123/modelview.js
@@ -11,7 +11,7 @@
1111
*
1212
* ModelView.js
1313
* @version: 4.0.6
14-
* @built on 2022-03-07 20:29:32
14+
* @built on 2022-03-08 10:08:22
1515
*
1616
* A simple, light-weight, versatile and fast isomorphic MVVM JavaScript framework (Browser and Server)
1717
* https://github.com/foo123/modelview.js
@@ -2123,7 +2123,7 @@ var
21232123
},
21242124
morphSelectedNodes = function morphSelectedNodes(view, r, v, start, end, end2, startv, count) {
21252125
var index, indexv, vnode, rnode, T, collection,
2126-
diff, di, dc, d, items, nodes,
2126+
diff, di, dc, d, items, keyed,
21272127
i, j, k, l, m, n, w, x, z, len, frag;
21282128
if ('collection' === v.childNodes[startv].nodeType)
21292129
{
@@ -2204,14 +2204,19 @@ var
22042204
return count;
22052205
}
22062206

2207-
for (nodes={},index=start; index<=end; index++)
2207+
keyed = {};
2208+
if (v.childNodes[startv].id || v.childNodes[stdMath.min(v.childNodes.length-1, startv+end-start)].id)
22082209
{
2209-
if (index >= r.childNodes.length) break;
2210-
rnode = r.childNodes[index];
2211-
rnode[MV] = rnode[MV] || MV0();
2212-
// store the keyed nodes in a map
2213-
// to be retrieved easily below
2214-
if (rnode[MV].id) nodes['#'+rnode[MV].id] = rnode;
2210+
// there are keyed nodes, associate them in a map for reuse
2211+
for (index=start; index<=end; index++)
2212+
{
2213+
if (index >= r.childNodes.length) break;
2214+
rnode = r.childNodes[index];
2215+
rnode[MV] = rnode[MV] || MV0();
2216+
// store the keyed nodes in a map
2217+
// to be retrieved and reused easily
2218+
if (rnode[MV].id) keyed['#'+rnode[MV].id] = rnode;
2219+
}
22152220
}
22162221
for (indexv=startv,index=start; index<=end; index++,indexv++)
22172222
{
@@ -2237,9 +2242,9 @@ var
22372242
{
22382243
morphSingle(view, r, rnode, vnode);
22392244
}
2240-
else if (vnode.id && (frag=nodes['#'+vnode.id]) && eqNodes(frag, vnode))
2245+
else if (vnode.id && (frag=keyed['#'+vnode.id]) && eqNodes(frag, vnode))
22412246
{
2242-
r.replaceChild(frag, rnode);
2247+
r.insertBefore(frag, rnode);
22432248
morphSingle(view, r, frag, vnode);
22442249
}
22452250
else if (0 === count)
@@ -2298,7 +2303,7 @@ var
22982303
// morph r (real) DOM to match v (virtual) DOM
22992304
var vc = v.childNodes.length, vpc = v.potentialChildNodes,
23002305
count = 0, offset = 0, matched, match,
2301-
mi, m, mc, di, dc, i, j, index, nodes,
2306+
mi, m, mc, di, dc, i, j, index, keyed,
23022307
vnode, rnode, T, frag, unconditionally,
23032308
rmv = r[MV] || MV0(),
23042309
modifiedNodesPrev = rmv.mod,
@@ -2377,15 +2382,24 @@ var
23772382
if ('collection' === v.childNodes[index].nodeType)
23782383
v.childNodes.splice.apply(v.childNodes, [index, 1].concat(htmlNode(view, '', null, null, [], v.childNodes[index].nodeValue.mapped()).childNodes));
23792384
}
2380-
for (nodes={},index=0,count=r.childNodes.length; index<count; index++)
2385+
vc = v.childNodes.length;
2386+
keyed = {};
2387+
for (index=0; index<vc; index++)
23812388
{
2382-
rnode = r.childNodes[index];
2383-
rnode[MV] = rnode[MV] || MV0();
2384-
// store the keyed nodes in a map
2385-
// to be retrieved easily below
2386-
if (rnode[MV].id) nodes['#'+rnode[MV].id] = rnode;
2389+
if (v.childNodes[index].id)
2390+
{
2391+
// there are keyed nodes, associate them in a map for reuse
2392+
for (index=0,count=r.childNodes.length; index<count; index++)
2393+
{
2394+
rnode = r.childNodes[index];
2395+
rnode[MV] = rnode[MV] || MV0();
2396+
// store the keyed nodes in a map
2397+
// to be retrieved and reused easily
2398+
if (rnode[MV].id) keyed['#'+rnode[MV].id] = rnode;
2399+
}
2400+
break;
2401+
}
23872402
}
2388-
vc = v.childNodes.length;
23892403
count = r.childNodes.length - vc;
23902404
for (index=0; index<vc; index++)
23912405
{
@@ -2403,9 +2417,9 @@ var
24032417
{
24042418
morphSingle(view, r, rnode, vnode, unconditionally);
24052419
}
2406-
else if (vnode.id && (frag=nodes['#'+vnode.id]) && eqNodes(frag, vnode))
2420+
else if (vnode.id && (frag=keyed['#'+vnode.id]) && eqNodes(frag, vnode))
24072421
{
2408-
r.replaceChild(frag, rnode);
2422+
r.insertBefore(frag, rnode);
24092423
morphSingle(view, r, frag, vnode, unconditionally);
24102424
}
24112425
else if (0 === count)

build/modelview.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,7 @@ var
20752075
},
20762076
morphSelectedNodes = function morphSelectedNodes(view, r, v, start, end, end2, startv, count) {
20772077
var index, indexv, vnode, rnode, T, collection,
2078-
diff, di, dc, d, items, nodes,
2078+
diff, di, dc, d, items, keyed,
20792079
i, j, k, l, m, n, w, x, z, len, frag;
20802080
if ('collection' === v.childNodes[startv].nodeType)
20812081
{
@@ -2156,14 +2156,19 @@ var
21562156
return count;
21572157
}
21582158

2159-
for (nodes={},index=start; index<=end; index++)
2159+
keyed = {};
2160+
if (v.childNodes[startv].id || v.childNodes[stdMath.min(v.childNodes.length-1, startv+end-start)].id)
21602161
{
2161-
if (index >= r.childNodes.length) break;
2162-
rnode = r.childNodes[index];
2163-
rnode[MV] = rnode[MV] || MV0();
2164-
// store the keyed nodes in a map
2165-
// to be retrieved easily below
2166-
if (rnode[MV].id) nodes['#'+rnode[MV].id] = rnode;
2162+
// there are keyed nodes, associate them in a map for reuse
2163+
for (index=start; index<=end; index++)
2164+
{
2165+
if (index >= r.childNodes.length) break;
2166+
rnode = r.childNodes[index];
2167+
rnode[MV] = rnode[MV] || MV0();
2168+
// store the keyed nodes in a map
2169+
// to be retrieved and reused easily
2170+
if (rnode[MV].id) keyed['#'+rnode[MV].id] = rnode;
2171+
}
21672172
}
21682173
for (indexv=startv,index=start; index<=end; index++,indexv++)
21692174
{
@@ -2189,9 +2194,9 @@ var
21892194
{
21902195
morphSingle(view, r, rnode, vnode);
21912196
}
2192-
else if (vnode.id && (frag=nodes['#'+vnode.id]) && eqNodes(frag, vnode))
2197+
else if (vnode.id && (frag=keyed['#'+vnode.id]) && eqNodes(frag, vnode))
21932198
{
2194-
r.replaceChild(frag, rnode);
2199+
r.insertBefore(frag, rnode);
21952200
morphSingle(view, r, frag, vnode);
21962201
}
21972202
else if (0 === count)
@@ -2250,7 +2255,7 @@ var
22502255
// morph r (real) DOM to match v (virtual) DOM
22512256
var vc = v.childNodes.length, vpc = v.potentialChildNodes,
22522257
count = 0, offset = 0, matched, match,
2253-
mi, m, mc, di, dc, i, j, index, nodes,
2258+
mi, m, mc, di, dc, i, j, index, keyed,
22542259
vnode, rnode, T, frag, unconditionally,
22552260
rmv = r[MV] || MV0(),
22562261
modifiedNodesPrev = rmv.mod,
@@ -2329,15 +2334,24 @@ var
23292334
if ('collection' === v.childNodes[index].nodeType)
23302335
v.childNodes.splice.apply(v.childNodes, [index, 1].concat(htmlNode(view, '', null, null, [], v.childNodes[index].nodeValue.mapped()).childNodes));
23312336
}
2332-
for (nodes={},index=0,count=r.childNodes.length; index<count; index++)
2337+
vc = v.childNodes.length;
2338+
keyed = {};
2339+
for (index=0; index<vc; index++)
23332340
{
2334-
rnode = r.childNodes[index];
2335-
rnode[MV] = rnode[MV] || MV0();
2336-
// store the keyed nodes in a map
2337-
// to be retrieved easily below
2338-
if (rnode[MV].id) nodes['#'+rnode[MV].id] = rnode;
2341+
if (v.childNodes[index].id)
2342+
{
2343+
// there are keyed nodes, associate them in a map for reuse
2344+
for (index=0,count=r.childNodes.length; index<count; index++)
2345+
{
2346+
rnode = r.childNodes[index];
2347+
rnode[MV] = rnode[MV] || MV0();
2348+
// store the keyed nodes in a map
2349+
// to be retrieved and reused easily
2350+
if (rnode[MV].id) keyed['#'+rnode[MV].id] = rnode;
2351+
}
2352+
break;
2353+
}
23392354
}
2340-
vc = v.childNodes.length;
23412355
count = r.childNodes.length - vc;
23422356
for (index=0; index<vc; index++)
23432357
{
@@ -2355,9 +2369,9 @@ var
23552369
{
23562370
morphSingle(view, r, rnode, vnode, unconditionally);
23572371
}
2358-
else if (vnode.id && (frag=nodes['#'+vnode.id]) && eqNodes(frag, vnode))
2372+
else if (vnode.id && (frag=keyed['#'+vnode.id]) && eqNodes(frag, vnode))
23592373
{
2360-
r.replaceChild(frag, rnode);
2374+
r.insertBefore(frag, rnode);
23612375
morphSingle(view, r, frag, vnode, unconditionally);
23622376
}
23632377
else if (0 === count)

0 commit comments

Comments
 (0)