From 577fe98e06b38a51b5e42240c6a6eea104cca090 Mon Sep 17 00:00:00 2001 From: Kabir Shah Date: Tue, 28 Mar 2017 20:56:32 -0700 Subject: [PATCH] :zap: perf boost: handle case when there are no children and remove all of them --- dist/moon.js | 18 ++++++++++++++---- dist/moon.min.js | 2 +- src/util/vdom.js | 18 ++++++++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/dist/moon.js b/dist/moon.js index 525b00f4..65057621 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -767,10 +767,20 @@ newText.meta.el = node.firstChild; } } else { - // Just Iterate through All Children - var totalLen = newLength > oldLength ? newLength : oldLength; - for (var i = 0; i < totalLen; i++) { - diff(oldVNode.children[i], vnode.children[i], node, instance); + if (newLength === 0) { + // No Children, Remove all Children if not Already Removed + if (oldLength !== 0) { + var firstChild = null; + while (firstChild = node.firstChild) { + removeChild(firstChild, node); + } + } + } else { + // Traverse and Diff Children + var totalLen = newLength > oldLength ? newLength : oldLength; + for (var i = 0; i < totalLen; i++) { + diff(oldVNode.children[i], vnode.children[i], node, instance); + } } } diff --git a/dist/moon.min.js b/dist/moon.min.js index 4adc872a..e3dbd888 100644 --- a/dist/moon.min.js +++ b/dist/moon.min.js @@ -5,4 +5,4 @@ * Free to use under the MIT license. * https://kingpixil.github.io/license */ -!function(e,t){"object"==typeof module&&module.exports?module.exports=t():e.Moon=t()}(this,function(){"use strict";function e(e){this.instance=e,this.cache={},this.signals={},this.dep={target:null,map:{}}}function t(t){this.$opts=t||{},this.$id=s++,this.$name=this.$opts.name||"root",this.$data=this.$opts.data||{},this.$render=this.$opts.render||$,this.$hooks=this.$opts.hooks||{},this.$methods=this.$opts.methods||{},this.$events={},this.$dom={},this.$observer=new e(this),this.$destroyed=!1,this.$initialRender=!0,this.$queued=!1,this.$opts.computed&&a(this,this.$opts.computed),this.init()}var r={},n={},i={},o={stop:"event.stopPropagation();",prevent:"event.preventDefault();",ctrl:"if(!event.ctrlKey) {return;};",shift:"if(!event.shiftKey) {return;};",alt:"if(!event.altKey) {return;};",enter:"if(event.keyCode !== 13) {return;};"},s=0,a=function(e,t){for(var r in t)!function(r){e.$observer.observe(r);var n={get:function(){var n=null;return e.$observer.cache[r]?n=e.$observer.cache[r]:(e.$observer.dep.target=r,n=t[r].get.call(e),e.$observer.dep.target=null,e.$observer.cache[r]=n),n}};t[r].set&&(n.set=function(n){return t[r].set.call(e,n)}),Object.defineProperty(e.$data,r,n)}(r)};e.prototype.observe=function(e){var t=this;this.signals[e]=function(){t.cache[e]=null}},e.prototype.notify=function(e){if(this.dep.map[e])for(var t=0;ta?s:a,p=0;p",e.current);if(n===-1)return e.tokens.push({type:"comment",value:t.slice(e.current)}),void(e.current=r);e.tokens.push({type:"comment",value:t.slice(e.current,n)}),e.current=n+3},N=function(e){var t=e.input,r=(t.length,"/"===t.charAt(e.current+1));e.tokens.push({type:"tagStart",close:r}),e.current+=r?2:1;var n=V(e);P(e);var i="/"===t.charAt(e.current);e.tokens.push({type:"tagEnd",close:!1}),e.current+=i?2:1,i&&(e.tokens.push({type:"tagStart",close:!0}),e.tokens.push({type:"tag",value:n}),e.tokens.push({type:"attribute",value:{}}),e.tokens.push({type:"tagEnd",close:!1}))},V=function(e){for(var t=e.input,r=t.length,n=e.current;n"!==i&&" "!==i)break;n++}for(var o=n;o"===i||" "===i)break;o++}var s=t.slice(n,o);return e.tokens.push({type:"tag",value:s}),e.current=o,s},P=function(e){for(var t=e.input,r=t.length,n=e.current,i={},o=t.charAt(n),s=t.charAt(n+1),a=function(){n++,o=t.charAt(n),s=t.charAt(n+1)};n"!==o&&("/"!==o||">"!==s);)if(" "!==o){for(var u="",l=!1;"="!==o&&n"!==o||"/"===o&&">"!==s)){l=!0;break}u+=o,a()}var c={name:u,value:"",meta:{}};if(l)i[u]=c;else{var p=" ";for(a(),"'"===o||'"'===o?(p=o,a()):c.value+=o;(o!==p&&">"!==o||"/"===o&&">"!==s)&&n1&&(a=u.shift(),s=u.join("(").slice(0,-1));var l="";n.shift();for(var c=0;ca?s:a,f=0;f",e.current);if(n===-1)return e.tokens.push({type:"comment",value:t.slice(e.current)}),void(e.current=r);e.tokens.push({type:"comment",value:t.slice(e.current,n)}),e.current=n+3},N=function(e){var t=e.input,r=(t.length,"/"===t.charAt(e.current+1));e.tokens.push({type:"tagStart",close:r}),e.current+=r?2:1;var n=V(e);P(e);var i="/"===t.charAt(e.current);e.tokens.push({type:"tagEnd",close:!1}),e.current+=i?2:1,i&&(e.tokens.push({type:"tagStart",close:!0}),e.tokens.push({type:"tag",value:n}),e.tokens.push({type:"attribute",value:{}}),e.tokens.push({type:"tagEnd",close:!1}))},V=function(e){for(var t=e.input,r=t.length,n=e.current;n"!==i&&" "!==i)break;n++}for(var o=n;o"===i||" "===i)break;o++}var s=t.slice(n,o);return e.tokens.push({type:"tag",value:s}),e.current=o,s},P=function(e){for(var t=e.input,r=t.length,n=e.current,i={},o=t.charAt(n),s=t.charAt(n+1),a=function(){n++,o=t.charAt(n),s=t.charAt(n+1)};n"!==o&&("/"!==o||">"!==s);)if(" "!==o){for(var u="",l=!1;"="!==o&&n"!==o||"/"===o&&">"!==s)){l=!0;break}u+=o,a()}var c={name:u,value:"",meta:{}};if(l)i[u]=c;else{var p=" ";for(a(),"'"===o||'"'===o?(p=o,a()):c.value+=o;(o!==p&&">"!==o||"/"===o&&">"!==s)&&n1&&(a=u.shift(),s=u.join("(").slice(0,-1));var l="";n.shift();for(var c=0;c oldLength ? newLength : oldLength; - for(var i = 0; i < totalLen; i++) { - diff(oldVNode.children[i], vnode.children[i], node, instance); + if(newLength === 0) { + // No Children, Remove all Children if not Already Removed + if(oldLength !== 0) { + let firstChild = null; + while((firstChild = node.firstChild)) { + removeChild(firstChild, node); + } + } + } else { + // Traverse and Diff Children + let totalLen = newLength > oldLength ? newLength : oldLength; + for(var i = 0; i < totalLen; i++) { + diff(oldVNode.children[i], vnode.children[i], node, instance); + } } }