jenkins-bot has submitted this change and it was merged.

Change subject: Replace some $.hasClass with Element.classList.contains
......................................................................


Replace some $.hasClass with Element.classList.contains

$.hasClass takes a few ms which is 10x than the ES5 method.
In these instances it is called many times per transaction,
keystroke or in a polling loop so it is worth optimising.

Change-Id: If6e61245593987a4c7baccfa0974c8cb2032c001
---
M src/ce/ve.ce.Document.js
M src/ce/ve.ce.FocusableNode.js
M src/ce/ve.ce.TextState.js
M src/ce/ve.ce.js
4 files changed, 35 insertions(+), 36 deletions(-)

Approvals:
  Divec: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/ce/ve.ce.Document.js b/src/ce/ve.ce.Document.js
index d9f5f27..0c277bb 100644
--- a/src/ce/ve.ce.Document.js
+++ b/src/ce/ve.ce.Document.js
@@ -192,8 +192,8 @@
        // TODO: remove this check: it can just be a case of 
non-branchNode/leafNode DOM element
        if ( slug && (
                !slug.firstChild ||
-               $( slug ).hasClass( 've-ce-branchNode-blockSlug' ) ||
-               $( slug.firstChild ).hasClass( 've-ce-chimera' )
+               slug.classList.contains( 've-ce-branchNode-blockSlug' ) ||
+               slug.firstChild.classList.contains( 've-ce-chimera' )
        ) ) {
                return { node: slug, offset: 0 };
        }
@@ -228,24 +228,24 @@
                        }
                } else if ( item.nodeType === Node.ELEMENT_NODE ) {
                        $item = current.$contents.eq( current.offset );
-                       if ( $item.hasClass( 've-ce-unicorn' ) ) {
+                       if ( item.classList.contains( 've-ce-unicorn' ) ) {
                                if ( offset === startOffset ) {
                                        // Return if empty unicorn pair at the 
correct offset
-                                       if ( $( $item[ 0 ].previousSibling 
).hasClass( 've-ce-unicorn' ) ) {
+                                       if ( item.previousSibling && 
item.previousSibling.classList.contains( 've-ce-unicorn' ) ) {
                                                return {
-                                                       node: $item[ 0 
].parentNode,
+                                                       node: item.parentNode,
                                                        offset: current.offset 
- 1
                                                };
-                                       } else if ( $( $item[ 0 ].nextSibling 
).hasClass( 've-ce-unicorn' ) ) {
+                                       } else if ( item.nextSibling && 
item.nextSibling.classList.contains( 've-ce-unicorn' ) ) {
                                                return {
-                                                       node: $item[ 0 
].parentNode,
+                                                       node: item.parentNode,
                                                        offset: current.offset 
+ 1
                                                };
                                        }
                                        // Else algorithm will/did descend into 
unicorned range
                                }
                                // Else algorithm will skip this unicorn
-                       } else if ( $item.is( '.ve-ce-branchNode, 
.ve-ce-leafNode' ) ) {
+                       } else if ( item.classList.contains( 've-ce-branchNode' 
) || item.classList.contains( 've-ce-leafNode' ) ) {
                                model = $item.data( 'view' ).model;
                                // DM nodes can render as multiple elements in 
the view, so check
                                // we haven't already counted it.
@@ -261,7 +261,7 @@
                                                startOffset += length;
                                        }
                                }
-                       } else if ( $item.hasClass( 
've-ce-branchNode-blockSlug' ) ) {
+                       } else if ( item.classList.contains( 
've-ce-branchNode-blockSlug' ) ) {
                                // This is unusual: generated wrappers usually 
mean that the return
                                // value of getBranchNodeFromOffset will not 
have block slugs or
                                // block slug ancestors before the offset 
position. However, there
@@ -271,7 +271,7 @@
                                // Skip contents without incrementing offset.
                                current.offset++;
                                continue;
-                       } else if ( $item.hasClass( 've-ce-nail' ) ) {
+                       } else if ( item.classList.contains( 've-ce-nail' ) ) {
                                // Skip contents without incrementing offset.
                                current.offset++;
                                continue;
diff --git a/src/ce/ve.ce.FocusableNode.js b/src/ce/ve.ce.FocusableNode.js
index 97b2878..ec29c7c 100644
--- a/src/ce/ve.ce.FocusableNode.js
+++ b/src/ce/ve.ce.FocusableNode.js
@@ -502,13 +502,14 @@
        }
 
        function process( el ) {
-               var i, j, il, jl, contained, clientRects, overflow,
-                       $el = $( el );
+               var i, j, il, jl, contained, clientRects, overflow, $el;
 
-               if ( $el.hasClass( 've-ce-noHighlight' ) ) {
+               if ( el.classList.contains( 've-ce-noHighlight' ) ) {
                        return;
                }
 
+               $el = $( el );
+
                if ( webkitColumns ) {
                        columnCount = $el.css( '-webkit-column-count' );
                        columnWidth = $el.css( '-webkit-column-width' );
diff --git a/src/ce/ve.ce.TextState.js b/src/ce/ve.ce.TextState.js
index 10fe182..1663f74 100644
--- a/src/ce/ve.ce.TextState.js
+++ b/src/ce/ve.ce.TextState.js
@@ -32,7 +32,7 @@
  * @return {ve.ce.TextStateChunk[]} chunks
  */
 ve.ce.TextState.static.getChunks = function ( element ) {
-       var $node, viewNode,
+       var viewNode,
                node = element,
                // Stack of element-lists in force; each element list is equal 
to its predecessor extended
                // by one element. This means two chunks have object-equal 
element lists if they have the
@@ -69,7 +69,6 @@
                // If appropriate, step into first child and loop
                // If no next sibling, step out until there is (breaking if we 
leave element)
                // Step to next sibling and loop
-               $node = $( node );
                // jscs:disable disallowEmptyBlocks
                if ( node.nodeType === Node.TEXT_NODE ) {
                        add( node.data.replace( /\u00A0/g, ' ' ) );
@@ -77,21 +76,21 @@
                        // Node types that don't appear in the model
                        // TODO: what about comments?
                        node.nodeType !== Node.ELEMENT_NODE ||
-                       $node.hasClass( 've-ce-branchNode-blockSlug' ) ||
-                       $node.hasClass( 've-ce-cursorHolder' )
+                       node.classList.contains( 've-ce-branchNode-blockSlug' ) 
||
+                       node.classList.contains( 've-ce-cursorHolder' )
                ) {
                        // Do nothing
-               } else if ( $node.hasClass( 've-ce-leafNode' ) ) {
+               } else if ( node.classList.contains( 've-ce-leafNode' ) ) {
                        // Don't return the content, but return placeholder 
characters so the
                        // offsets match up.
-                       viewNode = $node.data( 'view' );
+                       viewNode = $( node ).data( 'view' );
                        // Only return placeholders for the first element in a 
sibling group;
                        // otherwise we'll double count this node
                        if ( viewNode && node === viewNode.$element[ 0 ] ) {
                                // \u2603 is the snowman character: ☃
                                add( ve.repeatString( '\u2603', 
viewNode.getOuterLength() ) );
                        }
-               } else if ( $node.hasClass( 've-ce-unicorn' ) ) {
+               } else if ( node.classList.contains( 've-ce-unicorn' ) ) {
                        add( '', 'unicorn' );
                } else if ( node.firstChild ) {
                        if ( ve.ce.isAnnotationElement( node ) ) {
diff --git a/src/ce/ve.ce.js b/src/ce/ve.ce.js
index 73997fd..25235f1 100644
--- a/src/ce/ve.ce.js
+++ b/src/ce/ve.ce.js
@@ -41,7 +41,6 @@
        var func = function ( element ) {
                var viewNode,
                        nodeType = element.nodeType,
-                       $element = $( element ),
                        text = '';
 
                if (
@@ -49,18 +48,18 @@
                        nodeType === Node.DOCUMENT_NODE ||
                        nodeType === Node.DOCUMENT_FRAGMENT_NODE
                ) {
-                       if ( $element.hasClass( 've-ce-branchNode-blockSlug' ) 
) {
+                       if ( element.classList.contains( 
've-ce-branchNode-blockSlug' ) ) {
                                // Block slugs are not represented in the model 
at all, but they do
                                // contain a single nbsp/FEFF character in the 
DOM, so make sure
                                // that character isn't counted
                                return '';
-                       } else if ( $element.hasClass( 've-ce-cursorHolder' ) ) 
{
+                       } else if ( element.classList.contains( 
've-ce-cursorHolder' ) ) {
                                // Cursor holders do not exist in the model
                                return '';
-                       } else if ( $element.hasClass( 've-ce-leafNode' ) ) {
+                       } else if ( element.classList.contains( 
've-ce-leafNode' ) ) {
                                // For leaf nodes, don't return the content, 
but return
                                // the right number of placeholder characters 
so the offsets match up.
-                               viewNode = $element.data( 'view' );
+                               viewNode = $( element ).data( 'view' );
                                // Only return snowmen for the first element in 
a sibling group: otherwise
                                // we'll double-count this node
                                if ( viewNode && element === viewNode.$element[ 
0 ] ) {
@@ -107,9 +106,9 @@
        } else if ( nodeType === Node.ELEMENT_NODE || nodeType === 
Node.DOCUMENT_NODE ) {
                $element = $( element );
                if ( !(
-                       $element.hasClass( 've-ce-branchNode-blockSlug' ) ||
-                       $element.hasClass( 've-ce-cursorHolder' ) ||
-                       $element.hasClass( 've-ce-nail' )
+                       element.classList.contains( 
've-ce-branchNode-blockSlug' ) ||
+                       element.classList.contains( 've-ce-cursorHolder' ) ||
+                       element.classList.contains( 've-ce-nail' )
                ) ) {
                        hash += '<' + nodeName + '>';
                        // Traverse its children
@@ -176,14 +175,14 @@
  * @throws {Error} domNode is not in document
  */
 ve.ce.getOffset = function ( domNode, domOffset ) {
-       var node, view, offset, startNode, maxOffset, lengthSum = 0,
-               $domNode = $( domNode );
+       var node, view, offset, startNode, maxOffset,
+               lengthSum = 0;
 
-       if ( $domNode.hasClass( 've-ce-unicorn' ) ) {
+       if ( domNode.nodeType === Node.ELEMENT_NODE && 
domNode.classList.contains( 've-ce-unicorn' ) ) {
                if ( domOffset !== 0 ) {
                        throw new Error( 'Non-zero offset in unicorn' );
                }
-               return $domNode.data( 'modelOffset' );
+               return $( domNode ).data( 'modelOffset' );
        }
 
        /**
@@ -283,8 +282,8 @@
        } else {
                // Text inside of a block slug doesn't count
                if ( !(
-                       $( domNode.parentNode ).hasClass( 
've-ce-branchNode-blockSlug' ) ||
-                       $( domNode.parentNode ).hasClass( 've-ce-cursorHolder' )
+                       domNode.parentNode.classList.contains( 
've-ce-branchNode-blockSlug' ) ||
+                       domNode.parentNode.classList.contains( 
've-ce-cursorHolder' )
                ) ) {
                        lengthSum += domOffset;
                }
@@ -308,8 +307,8 @@
                // Text inside of a block slug doesn't count
                if (
                        node.nodeType === Node.TEXT_NODE &&
-                       !$( node.parentNode ).hasClass( 
've-ce-branchNode-blockSlug' ) &&
-                       !$( node.parentNode ).hasClass( 've-ce-cursorHolder' )
+                       !node.parentNode.classList.contains( 
've-ce-branchNode-blockSlug' ) &&
+                       !node.parentNode.classList.contains( 
've-ce-cursorHolder' )
                ) {
                        lengthSum += node.data.length;
                }

-- 
To view, visit https://gerrit.wikimedia.org/r/270790
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If6e61245593987a4c7baccfa0974c8cb2032c001
Gerrit-PatchSet: 2
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>
Gerrit-Reviewer: Divec <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to