Skip to content

Commit 4d2bd24

Browse files
committed
Widget: Optimize attachment of the _untrackClassesElement listener
jQuery UI 1.13.0 changed the logic attaching the `_untrackClassesElement` listener in the `_classes` widget method; one of the side effects was calling `this._on` for each node that needed the listener. That caused a severe performance degradation for large comboboxes as each `_on` jQuery UI call causes a jQuery `add` call that calls Sizzle's `uniqueSort` underneath. Instead, collect the nodes that need the listener and then, outside of the loop, create a jQuery object out of them and attach the listener once. That's still slower than the jQuery 1.12 version but only slightly: 936 ms to 1.03s on a very large list on a recent MacBook Pro, compared to ~30 seconds before this patch. Fixes jquerygh-2014
1 parent e90096e commit 4d2bd24

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

ui/widget.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ $.Widget.prototype = {
499499
}, options );
500500

501501
function bindRemoveEvent() {
502+
var nodesToBind = [];
503+
502504
options.element.each( function( _, element ) {
503505
var isTracked = $.map( that.classesElementLookup, function( elements ) {
504506
return elements;
@@ -508,11 +510,13 @@ $.Widget.prototype = {
508510
} );
509511

510512
if ( !isTracked ) {
511-
that._on( $( element ), {
512-
remove: "_untrackClassesElement"
513-
} );
513+
nodesToBind.push( element );
514514
}
515515
} );
516+
517+
that._on( $( nodesToBind ), {
518+
remove: "_untrackClassesElement"
519+
} );
516520
}
517521

518522
function processClassString( classes, checkOption ) {

0 commit comments

Comments
 (0)