Skip to content

Commit 8e29a3f

Browse files
committed
skip valuediffing in nodeToObj when not needed for speed increase
1 parent 2a2c4dd commit 8e29a3f

File tree

2 files changed

+84
-85
lines changed

2 files changed

+84
-85
lines changed

diffDOM.js

Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -254,82 +254,6 @@
254254
//return obj;
255255
};
256256

257-
var nodeToObj = function(node) {
258-
var objNode = {};
259-
objNode.nodeName = node.nodeName;
260-
if (objNode.nodeName === '#text' || objNode.nodeName === '#comment') {
261-
objNode.data = node.data;
262-
} else {
263-
if (node.attributes && node.attributes.length > 0) {
264-
objNode.attributes = {};
265-
Array.prototype.slice.call(node.attributes).forEach(
266-
function(attribute) {
267-
objNode.attributes[attribute.name] = attribute.value;
268-
}
269-
);
270-
}
271-
if (node.childNodes && node.childNodes.length > 0) {
272-
objNode.childNodes = [];
273-
Array.prototype.slice.call(node.childNodes).forEach(
274-
function(childNode) {
275-
objNode.childNodes.push(nodeToObj(childNode));
276-
}
277-
);
278-
}
279-
if (node.value) {
280-
objNode.value = node.value;
281-
}
282-
if (node.checked) {
283-
objNode.checked = node.checked;
284-
}
285-
if (node.selected) {
286-
objNode.selected = node.selected;
287-
}
288-
}
289-
290-
return objNode;
291-
};
292-
293-
294-
var objToNode = function(objNode, insideSvg) {
295-
var node;
296-
if (objNode.nodeName === '#text') {
297-
node = document.createTextNode(objNode.data);
298-
299-
} else if (objNode.nodeName === '#comment') {
300-
node = document.createComment(objNode.data);
301-
} else {
302-
if (objNode.nodeName === 'svg' || insideSvg) {
303-
node = document.createElementNS('http://www.w3.org/2000/svg', objNode.nodeName);
304-
insideSvg = true;
305-
} else {
306-
node = document.createElement(objNode.nodeName);
307-
}
308-
if (objNode.attributes) {
309-
Object.keys(objNode.attributes).forEach(function(attribute) {
310-
node.setAttribute(attribute, objNode.attributes[attribute]);
311-
});
312-
}
313-
if (objNode.childNodes) {
314-
objNode.childNodes.forEach(function(childNode) {
315-
node.appendChild(objToNode(childNode, insideSvg));
316-
});
317-
}
318-
if (objNode.value) {
319-
node.value = objNode.value;
320-
}
321-
if (objNode.checked) {
322-
node.checked = objNode.checked;
323-
}
324-
if (objNode.selected) {
325-
node.selected = objNode.selected;
326-
}
327-
}
328-
return node;
329-
};
330-
331-
332-
333257
/**
334258
* based on https://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#JavaScript
335259
*/
@@ -537,14 +461,14 @@
537461

538462
diff: function(t1Node, t2Node) {
539463

540-
var t1 = nodeToObj(t1Node),
541-
t2 = nodeToObj(t2Node);
464+
var t1 = this.nodeToObj(t1Node),
465+
t2 = this.nodeToObj(t2Node);
542466

543467
diffcount = 0;
544468

545469
if (this.debug) {
546-
this.t1Orig = nodeToObj(t1Node);
547-
this.t2Orig = nodeToObj(t2Node);
470+
this.t1Orig = this.nodeToObj(t1Node);
471+
this.t2Orig = this.nodeToObj(t2Node);
548472
}
549473

550474
this.tracker = new DiffTracker();
@@ -694,6 +618,81 @@
694618

695619
return diffs;
696620
},
621+
nodeToObj: function(node) {
622+
var objNode = {}, dobj = this;
623+
objNode.nodeName = node.nodeName;
624+
if (objNode.nodeName === '#text' || objNode.nodeName === '#comment') {
625+
objNode.data = node.data;
626+
} else {
627+
if (node.attributes && node.attributes.length > 0) {
628+
objNode.attributes = {};
629+
Array.prototype.slice.call(node.attributes).forEach(
630+
function(attribute) {
631+
objNode.attributes[attribute.name] = attribute.value;
632+
}
633+
);
634+
}
635+
if (node.childNodes && node.childNodes.length > 0) {
636+
objNode.childNodes = [];
637+
Array.prototype.slice.call(node.childNodes).forEach(
638+
function(childNode) {
639+
objNode.childNodes.push(dobj.nodeToObj(childNode));
640+
}
641+
);
642+
}
643+
if (this.valueDiffing) {
644+
if (node.value) {
645+
objNode.value = node.value;
646+
}
647+
if (node.checked) {
648+
objNode.checked = node.checked;
649+
}
650+
if (node.selected) {
651+
objNode.selected = node.selected;
652+
}
653+
}
654+
}
655+
656+
return objNode;
657+
},
658+
objToNode: function(objNode, insideSvg) {
659+
var node, dobj = this;
660+
if (objNode.nodeName === '#text') {
661+
node = document.createTextNode(objNode.data);
662+
663+
} else if (objNode.nodeName === '#comment') {
664+
node = document.createComment(objNode.data);
665+
} else {
666+
if (objNode.nodeName === 'svg' || insideSvg) {
667+
node = document.createElementNS('http://www.w3.org/2000/svg', objNode.nodeName);
668+
insideSvg = true;
669+
} else {
670+
node = document.createElement(objNode.nodeName);
671+
}
672+
if (objNode.attributes) {
673+
Object.keys(objNode.attributes).forEach(function(attribute) {
674+
node.setAttribute(attribute, objNode.attributes[attribute]);
675+
});
676+
}
677+
if (objNode.childNodes) {
678+
objNode.childNodes.forEach(function(childNode) {
679+
node.appendChild(dobj.objToNode(childNode, insideSvg));
680+
});
681+
}
682+
if (this.valueDiffing) {
683+
if (objNode.value) {
684+
node.value = objNode.value;
685+
}
686+
if (objNode.checked) {
687+
node.checked = objNode.checked;
688+
}
689+
if (objNode.selected) {
690+
node.selected = objNode.selected;
691+
}
692+
}
693+
}
694+
return node;
695+
},
697696
findInnerDiff: function(t1, t2, route) {
698697

699698
var subtrees = (t1.childNodes && t2.childNodes) ? markSubTrees(t1, t2) : [],
@@ -933,10 +932,10 @@
933932
diffs.forEach(function(diff) {
934933
// console.log(JSON.stringify(diff));
935934
// console.log(JSON.stringify(tree));
936-
// console.log(objToNode(tree).outerHTML);
935+
// console.log(this.objToNode(tree).outerHTML);
937936
dobj.applyVirtualDiff(tree, diff);
938937
// console.log(JSON.stringify(tree));
939-
// console.log(objToNode(tree).outerHTML);
938+
// console.log(this.objToNode(tree).outerHTML);
940939
});
941940
return true;
942941
},
@@ -1177,7 +1176,7 @@
11771176
node.selected = diff.newValue;
11781177
break;
11791178
case 'replaceElement':
1180-
node.parentNode.replaceChild(objToNode(diff.newValue), node);
1179+
node.parentNode.replaceChild(this.objToNode(diff.newValue), node);
11811180
break;
11821181
case 'relocateGroup':
11831182
Array.apply(null, new Array(diff.groupLength)).map(function() {
@@ -1196,7 +1195,7 @@
11961195
route = diff.route.slice();
11971196
c = route.splice(route.length - 1, 1)[0];
11981197
node = this.getFromRoute(tree, route);
1199-
node.insertBefore(objToNode(diff.element), node.childNodes[c]);
1198+
node.insertBefore(this.objToNode(diff.element), node.childNodes[c]);
12001199
break;
12011200
case 'removeTextElement':
12021201
if (!node || node.nodeType !== 3) {

tests/site-integration.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
if(diff && this._VDOM){
230230
this._VDOM = this[0].cloneNode();
231231
//console.log(this._VDOM);
232-
this._VDD = this._VDD || new window.diffDOM({valueDiffing: false,debug:true,diffcap:500});
232+
this._VDD = this._VDD || new window.diffDOM({valueDiffing: false});
233233
this._VDOM.innerHTML = html;
234234
return applyDiff.call(this);
235235
}else{

0 commit comments

Comments
 (0)