Optimisations on init, filtering, dropdown show/hide after profiling 20,000 options. #1103
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I profiled the plugin with 20,000 options and created the following optimisations:
(I've committed each optimisation separately for easy referencing.)
a) Optimised getSelected used in updateButtonText
The jQuery filter was causing a 16 second delay, changing it to the following removed it:
return $(this.$select.get(0).selectedOptions)
selectedOptions is supported in all major browsers except for IE (Edge supports it) so I also implemented a fall back.
b) Optimised buildDropdownOptions
This function was causing the longest delay when loading (nearly 30 seconds).
I've reduced it to under a second by building a string array and then creating the list in one shot.
I disabled dividers and the check for disabled options. You may want to develop further to support these.
Also see XSS fix.
c) Optimised dropdown show/hide
The plugin uses bootstrap drop-downs that toggles the .open style on the dropdown when the button is clicked.
Originally this toggled from display: none to display: block.
Doing so causes a reflow which can take 3 seconds to open and 15 seconds to close on 20000 options.
Toggling visibility:hidden and height:0 instead reduces the delays to about 380ms.
d) Optimised search
This was similar to c), the plugin set display:none or display:block on each list element when filtering.
I removed this and toggled height instead.
This reduced the delay from 1.4 minutes on one search to 800ms.