From 2e1cbc3a7d5a154eae8cdaf5db8671ab5be871e6 Mon Sep 17 00:00:00 2001 From: austinshenk Date: Wed, 24 Oct 2018 13:22:32 -0400 Subject: [PATCH] Convert Kernel's for-loops to use faster conditional and cache length --- src/Elm/Kernel/VirtualDom.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Elm/Kernel/VirtualDom.js b/src/Elm/Kernel/VirtualDom.js index 414a820..b45c969 100644 --- a/src/Elm/Kernel/VirtualDom.js +++ b/src/Elm/Kernel/VirtualDom.js @@ -462,7 +462,8 @@ function _VirtualDom_render(vNode, eventNode) _VirtualDom_applyFacts(domNode, eventNode, vNode.__facts); - for (var kids = vNode.__kids, i = 0; i < kids.length; i++) + var len = kids.length; + for (var kids = vNode.__kids, i = 0; len - i; i++) { _VirtualDom_appendChild(domNode, _VirtualDom_render(tag === __2_NODE ? kids[i] : kids[i].b, eventNode)); } @@ -833,7 +834,8 @@ function _VirtualDom_diffHelp(x, y, patches, index) // assumes the incoming arrays are the same length function _VirtualDom_pairwiseRefEqual(as, bs) { - for (var i = 0; i < as.length; i++) + var len = as.length; + for (var i = 0; len - i; i++) { if (as[i] !== bs[i]) { @@ -964,7 +966,7 @@ function _VirtualDom_diffKids(xParent, yParent, patches, index) // PAIRWISE DIFF EVERYTHING ELSE - for (var minLen = xLen < yLen ? xLen : yLen, i = 0; i < minLen; i++) + for (var minLen = xLen < yLen ? xLen : yLen, i = 0; minLen - i; i++) { var xKid = xKids[i]; _VirtualDom_diffHelp(xKid, yKids[i], patches, ++index); @@ -994,7 +996,7 @@ function _VirtualDom_diffKeyedKids(xParent, yParent, patches, rootIndex) var index = rootIndex; - while (xIndex < xLen && yIndex < yLen) + while ( xLen - xIndex && yLen - yIndex) { var x = xKids[xIndex]; var y = yKids[yIndex]; @@ -1108,7 +1110,7 @@ function _VirtualDom_diffKeyedKids(xParent, yParent, patches, rootIndex) // eat up any remaining nodes with removeNode and insertNode - while (xIndex < xLen) + while (xLen - xIndex) { index++; var x = xKids[xIndex]; @@ -1118,7 +1120,7 @@ function _VirtualDom_diffKeyedKids(xParent, yParent, patches, rootIndex) xIndex++; } - while (yIndex < yLen) + while (yLen - yIndex) { var endInserts = endInserts || []; var y = yKids[yIndex]; @@ -1313,7 +1315,8 @@ function _VirtualDom_addDomNodesHelp(domNode, vNode, patches, i, low, high, even var vKids = vNode.__kids; var childNodes = domNode.childNodes; - for (var j = 0; j < vKids.length; j++) + var len = vKids.length; + for (var j = 0; len - j; j++) { low++; var vKid = tag === __2_NODE ? vKids[j] : vKids[j].b; @@ -1349,7 +1352,8 @@ function _VirtualDom_applyPatches(rootDomNode, oldVirtualNode, patches, eventNod function _VirtualDom_applyPatchesHelp(rootDomNode, patches) { - for (var i = 0; i < patches.length; i++) + var len = patches.length; + for (var i = 0; len - i; i++) { var patch = patches[i]; var localDomNode = patch.__domNode @@ -1467,7 +1471,8 @@ function _VirtualDom_applyPatchReorder(domNode, patch) // inserts var inserts = data.__inserts; - for (var i = 0; i < inserts.length; i++) + var len = inserts.length; + for (var i = 0; len - i; i++) { var insert = inserts[i]; var entry = insert.__entry; @@ -1495,7 +1500,8 @@ function _VirtualDom_applyPatchReorderEndInsertsHelp(endInserts, patch) } var frag = _VirtualDom_doc.createDocumentFragment(); - for (var i = 0; i < endInserts.length; i++) + var len = endInserts.length; + for (var i = 0; len - i; i++) { var insert = endInserts[i]; var entry = insert.__entry; @@ -1554,7 +1560,7 @@ function _VirtualDom_dekey(keyedNode) var keyedKids = keyedNode.__kids; var len = keyedKids.length; var kids = new Array(len); - for (var i = 0; i < len; i++) + for (var i = 0; len - i; i++) { kids[i] = keyedKids[i].b; }