Skip to content

Commit

Permalink
Widget: Optimize attachment of the _untrackClassesElement listener
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgol committed Jan 5, 2022
1 parent 933ce5d commit cd0fe70
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ui/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ $.Widget.prototype = {
}, options );

function bindRemoveEvent() {
var nodesToBind = [];

options.element.each( function( _, element ) {
var isTracked = $.map( that.classesElementLookup, function( elements ) {
return elements;
Expand All @@ -508,11 +510,13 @@ $.Widget.prototype = {
} );

if ( !isTracked ) {
that._on( $( element ), {
remove: "_untrackClassesElement"
} );
nodesToBind.push( element );
}
} );

that._on( $( nodesToBind ), {
remove: "_untrackClassesElement"
} );
}

function processClassString( classes, checkOption ) {
Expand Down

0 comments on commit cd0fe70

Please sign in to comment.