Skip to content

Commit 87070cc

Browse files
committed
Reclaiming another 700-800 msecs on the 400 listview item test for WP7.5 (Mango) with a few minor tweaks:
- Added _findFirstElementByTagName() which does basic DOM traversal to find the first of an element with the given nodeName. Use this in place of $.fn.closest() and $.fn.children() calls that filter with ":eq(0)". - Avoid calling $.fn.add() if you can. The creation of the new collection is costing about 400 msecs. - Avoid calling $() with markup for a single node, just use document.createElement() and pass it to $().
1 parent 8ef15e8 commit 87070cc

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

js/jquery.mobile.listview.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ $.widget( "mobile.listview", $.mobile.widget, {
9292
}
9393
},
9494

95+
// This is a generic utility method for finding the first
96+
// node with a given nodeName. It uses basic DOM traversal
97+
// to be fast and is meant to be a substitute for simple
98+
// $.fn.closest() and $.fn.children() calls on a single
99+
// element. Note that callers must pass both the lowerCase
100+
// and upperCase version of the nodeName they are looking for.
101+
// The main reason for this is that this function will be
102+
// called many times and we want to avoid having to lowercase
103+
// the nodeName from the element every time to ensure we have
104+
// a match. Note that this function lives here for now, but may
105+
// be moved into $.mobile if other components need a similar method.
106+
_findFirstElementByTagName: function( ele, nextProp, lcName, ucName )
107+
{
108+
var dict = {};
109+
dict[ lcName ] = dict[ ucName ] = true;
110+
while ( ele ) {
111+
if ( dict[ ele.nodeName ] ) {
112+
return ele;
113+
}
114+
ele = ele[ nextProp ];
115+
}
116+
return null;
117+
},
118+
119+
_addThumbClasses: function( containers )
120+
{
121+
var i, img, len = containers.length;
122+
for ( i = 0; i < len; i++ ) {
123+
img = $( this._findFirstElementByTagName( containers[ i ].firstChild, "nextSibling", "img", "IMG" ) );
124+
if ( img.length ) {
125+
img.addClass( "ui-li-thumb" );
126+
$( this._findFirstElementByTagName( img[ 0 ].parentNode, "parentNode", "li", "LI" ) ).addClass( img.is( ".ui-li-icon" ) ? "ui-li-has-icon" : "ui-li-has-thumb" );
127+
}
128+
}
129+
},
130+
95131
refresh: function( create ) {
96132
this.parentPage = this.element.closest( ".ui-page" );
97133
this._createSubPages();
@@ -157,7 +193,7 @@ $.widget( "mobile.listview", $.mobile.widget, {
157193
})
158194
.find( ".ui-btn-inner" )
159195
.append(
160-
$( "<span />" ).buttonMarkup({
196+
$( document.createElement( "span" ) ).buttonMarkup({
161197
shadow: true,
162198
corners: true,
163199
theme: splittheme,
@@ -188,7 +224,7 @@ $.widget( "mobile.listview", $.mobile.widget, {
188224
.prepend( "<span class='ui-li-dec'>" + (counter++) + ". </span>" );
189225
}
190226

191-
item.add( item.children( ".ui-btn-inner" ) ).addClass( itemClass );
227+
item.addClass( itemClass ).children( ".ui-btn-inner" ).addClass( itemClass );
192228
}
193229

194230
$list.find( "h1, h2, h3, h4, h5, h6" ).addClass( "ui-li-heading" )
@@ -219,14 +255,8 @@ $.widget( "mobile.listview", $.mobile.widget, {
219255
// allows the 400 listview item page to load in about 3 seconds as
220256
// opposed to 30 seconds.
221257

222-
imgParents = li.add( $list.find( ".ui-link-inherit" ) );
223-
224-
for ( pos = 0; pos < imgParents.length; pos++ ) {
225-
img = imgParents.eq( pos ).children( "img" ).first();
226-
if ( img.length ) {
227-
img.addClass( "ui-li-thumb" ).closest( "li" ).addClass( img.is( ".ui-li-icon" ) ? "ui-li-has-icon" : "ui-li-has-thumb" );
228-
}
229-
}
258+
this._addThumbClasses( li );
259+
this._addThumbClasses( $list.find( ".ui-link-inherit" ) );
230260

231261
this._refreshCorners( create );
232262
},

0 commit comments

Comments
 (0)