GWicke has uploaded a new change for review. https://gerrit.wikimedia.org/r/130278
Change subject: Small performance tweaks for commonly called functions ...................................................................... Small performance tweaks for commonly called functions * Use numeric constants instead of variable dereferences * locally define isElt * use the nodeType in isComment Change-Id: I8f3a784b089b90619809ebc49268a0be482ad753 --- M lib/mediawiki.DOMUtils.js 1 file changed, 28 insertions(+), 26 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid refs/changes/78/130278/1 diff --git a/lib/mediawiki.DOMUtils.js b/lib/mediawiki.DOMUtils.js index a083c99..f37509a 100644 --- a/lib/mediawiki.DOMUtils.js +++ b/lib/mediawiki.DOMUtils.js @@ -16,6 +16,10 @@ // define some constructor shortcuts var KV = pd.KV; +var isElt = function(node) { + return node.nodeType === 1; +}; + /** * @class * @singleton @@ -29,9 +33,7 @@ * See http://dom.spec.whatwg.org/#dom-node-nodetype * @param {Node} node */ - isElt: function(node) { - return node.nodeType === node.ELEMENT_NODE; - }, + isElt: isElt, /** * Check whether this is a DOM text node. @@ -39,7 +41,7 @@ * @param {Node} node */ isText: function(node) { - return node.nodeType === node.TEXT_NODE; + return node.nodeType === 3; }, /** @@ -182,7 +184,7 @@ * @param {Mixed} defaultVal What to use if there is no JSON attribute by that name. */ loadDataAttrib: function(node, name, defaultVal) { - if ( !this.isElt(node) ) { + if ( !isElt(node) ) { return; } var data = this.getNodeData( node ); @@ -196,7 +198,7 @@ * Save all node.data.* structures to data attributes */ saveDataAttribs: function(node) { - if ( !this.isElt(node) ) { + if ( !isElt(node) ) { return; } var data = this.getNodeData( node ); @@ -219,7 +221,7 @@ */ loadDataParsoid: function ( node ) { var dp = this.loadDataAttrib( node, 'parsoid', {} ); - if ( this.isElt( node ) && !dp.tmp ) { + if ( isElt( node ) && !dp.tmp ) { dp.tmp = {}; } }, @@ -268,7 +270,7 @@ * @param {Mixed} defaultVal What should be returned if we fail to find a valid JSON structure */ getJSONAttribute: function(node, name, defaultVal) { - if ( !this.isElt(node) ) { + if ( !isElt(node) ) { return defaultVal !== undefined ? defaultVal : {}; } @@ -448,7 +450,7 @@ * @param {string} type Expected value of "typeof" attribute */ isNodeOfType: function(n, name, type) { - return this.hasNodeName(n, name) && n.getAttribute("typeof") === type; + return n.nodeName.toLowerCase() === name && n.getAttribute("typeof") === type; }, /** @@ -539,7 +541,7 @@ */ isLiteralHTMLNode: function(node) { return (node && - this.isElt(node) && + isElt(node) && this.hasLiteralHTMLMarker(this.getDataParsoid(node))); }, @@ -573,7 +575,7 @@ getPrevElementSibling: function(node) { var sibling = node.previousSibling; while (sibling) { - if (this.isElt(sibling)) { + if (isElt(sibling)) { return sibling; } sibling = sibling.previousSibling; @@ -588,7 +590,7 @@ getNextElementSibling: function(node) { var sibling = node.nextSibling; while (sibling) { - if (this.isElt(sibling)) { + if (isElt(sibling)) { return sibling; } sibling = sibling.nextSibling; @@ -602,7 +604,7 @@ hasElementChild: function(node) { var children = node.childNodes; for (var i = 0, n = children.length; i < n; i++) { - if (this.isElt(children[i])) { + if (isElt(children[i])) { return true; } } @@ -617,7 +619,7 @@ var children = node.childNodes; for (var i = 0, n = children.length; i < n; i++) { var child = children[i]; - if (this.isElt(child) && + if (isElt(child) && // Is a block-level node ( this.isBlockNode(child) || // or has a block-level child or grandchild or.. @@ -683,7 +685,7 @@ * @param {Node} node */ isTplElementNode: function(env, node) { - if (this.isElt(node)) { + if (isElt(node)) { var about = node.getAttribute('about'); return about && env.isParsoidObjectId(about); } else { @@ -786,7 +788,7 @@ }, currentDiffMark: function(node, env) { - if (!node || !this.isElt(node)) { + if (!node || !isElt(node)) { return null; } var data = this.getNodeData( node ); @@ -1012,7 +1014,7 @@ * @param {string} someClass */ hasClass: function ( ele, someClass ) { - if ( !ele || !this.isElt(ele) ) { + if ( !ele || !isElt(ele) ) { return false; } @@ -1040,7 +1042,7 @@ treeHasElement: function(node, tagName) { node = node.firstChild; while (node) { - if (this.isElt(node)) { + if (isElt(node)) { if (node.nodeName === tagName || this.treeHasElement(node, tagName)) { return true; } @@ -1099,7 +1101,7 @@ * Is node the first wrapper element of encapsulated content? */ isFirstEncapsulationWrapperNode: function(node) { - return this.isElt(node) && (/(?:^|\s)mw:(?:Transclusion(?=$|\s)|Param(?=$|\s)|Extension\/[^\s]+)/).test(node.getAttribute('typeof')); + return isElt(node) && (/(?:^|\s)mw:(?:Transclusion(?=$|\s)|Param(?=$|\s)|Extension\/[^\s]+)/).test(node.getAttribute('typeof')); }, /** @@ -1113,7 +1115,7 @@ while ( !this.isFirstEncapsulationWrapperNode( node ) ) { node = node.previousSibling; - if ( !node || !this.isElt(node) || + if ( !node || !isElt(node) || node.getAttribute('about') !== about ) { return null; } @@ -1131,7 +1133,7 @@ // True if it has an encapsulation type or while walking backwards // over elts with identical about ids, we run into a node with an // encapsulation type. - if (!this.isElt(node)) { + if (!isElt(node)) { return false; } @@ -1161,8 +1163,8 @@ node = node.nextSibling; while (node && ( - this.isElt(node) && node.getAttribute('about') === about || - this.isFosterablePosition(node) && !this.isElt(node) && this.isIEW(node) + isElt(node) && node.getAttribute('about') === about || + this.isFosterablePosition(node) && !isElt(node) && this.isIEW(node) )) { nodes.push(node); @@ -1422,12 +1424,12 @@ function previousSiblingIsWrapper(sibling, about) { return sibling && - DU.isElt(sibling) && + isElt(sibling) && about === sibling.getAttribute("about") && hasRightType(sibling); } - if (!DU.isElt(node)) { + if (!isElt(node)) { return false; } @@ -1656,7 +1658,7 @@ }, isComment: function( node ) { - return this.hasNodeName( node, "#comment" ); + return node.nodeType === 8; }, // Applies a data-parsoid JSON structure to the document. -- To view, visit https://gerrit.wikimedia.org/r/130278 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8f3a784b089b90619809ebc49268a0be482ad753 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/parsoid Gerrit-Branch: master Gerrit-Owner: GWicke <gwi...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits