FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

JQuery "find" replaced with more efficient "querySelectorAll" in SelectAdapter by vyshkant · Pull Request #5775 · select2/select2 · GitHub

JQuery "find" replaced with more efficient "querySelectorAll" in SelectAdapter - #5775

Merged
kevin-brown merged 2 commits into
select2:developfrom
vyshkant:feature/find-optimizations
Apr 19, 2020
Merged

JQuery "find" replaced with more efficient "querySelectorAll" in SelectAdapter#5775
kevin-brown merged 2 commits into
select2:developfrom
vyshkant:feature/find-optimizations

Conversation

vyshkant commented Feb 16, 2020
edited
Loading

Copy link
Copy Markdown
Contributor

This pull request includes a

  • Bug fix
  • New feature
  • Translation

The following changes were made

  • in SelectAdapter's current method replaced expensive call to jQuery.find with more efficient call to DOMElement.querySelectorAll

According to W3C Recommendation 06 November 2018,

the :checked pseudo-class initially applies to such elements that have the HTML4 selected and checked attributes

which is applicable for our case.

Here is an example illustrating the problems of jQuery.find method: https://jsfiddle.net/vyshkant/nzr4kp8u/12/

CAUTION: an execution of the example might take about 2 minutes (which actually illustrates the degradation of jQuery.find with increasing the number of option tags.

In my personal case I have a middle-size db which includes about 20 000 of options and it is impossible to use Select2 because of the timing.

Copy link
Copy Markdown
Contributor Author

If you agree with my arguments and consider the PR appropriate, I'll gladly fix the tests.

Copy link
Copy Markdown
Member

This is something I'm willing to entertain, and the timing is great since we're about to start dropping legacy support for IE. The tests need to be fixed, as you've noted, but this looks like a solid start.

Copy link
Copy Markdown
Contributor Author

@kevin-brown do you have any ideas why did the tests fail?

I have the same message on my local computer:

Running "qunit:all" (qunit) task
Testing http://localhost:9999/tests/integration-jq1.html F
>> PhantomJS timed out, possibly due to:
>> - QUnit is not loaded correctly.
>> - A missing QUnit start() call.
>> - Or, a misconfiguration of this task.

Meanwhile at the develop branch everything is OK, even locally.

Maybe I need to (re)generate some files? Could you please help me understanding your workflow?)

Copy link
Copy Markdown
Contributor Author

And when I open QUnit in browser I can see tests are passed:

Comment thread src/js/select2/data/select.js Outdated

data.push(option);
});
for (var index in selectedElements) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is there a reason why you chose to use for ... in here? MDN explicitly recommends against using it for a NodeList, which is what is being returned by querySelectorAll.

https://developer.mozilla.org/en-US/docs/Web/API/NodeList

Comment thread src/js/select2/data/select.js Outdated
data.push(option);
});
for (var index in selectedElements) {
if (selectedElements.hasOwnProperty(index)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I suspect this is allowing non-DOMElement properties of the NodeList to make their way through in tests which is causing the test failures we are seeing in CI. I would recommend falling back to a traditional index-based for loop which should be fine for this situation and should ensure that we only ever try to call .item on a DOM element.

Copy link
Copy Markdown
Contributor Author

@kevin-brown you were absolutely right about iteration over NodeList, thanks a lot! Looks like the tests got fixed.

vyshkant requested a review from kevin-brown February 25, 2020 05:00

kevin-brown left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The changes in general look good, but the changes to the dist files need to be removed from the pull request. We recompile those files before every release separately, since otherwise we would run into a lot of merge conflicts within the files during reviews.

I also have one question about a guard that was included.

Comment thread src/js/select2/data/select.js Outdated
vyshkant requested a review from kevin-brown March 8, 2020 23:47

vyshkant commented Mar 8, 2020

Copy link
Copy Markdown
Contributor Author

@kevin-brown could you please take a look again? I've removed dist changes and modified current method of SelectAdapter (removed unnecessary check).

Copy link
Copy Markdown
Member

This looks good and it'll land in the next release (4.1.0) which is coming at some yet-to-be-determined date.

Right now the status checks are failing but that appears to be because of a GitHub Actions issue. I'll look into that later, but it shouldn't impact this.

kevin-brown merged commit cbadb0a into select2:develop Apr 19, 2020
maxwellfet928 pushed a commit to maxwellfet928/select2 that referenced this pull request Jun 8, 2026
* JQuery "find" replaced with more efficient "querySelectorAll"

* Refactored to use Array.prototype.map

Co-authored-by: Kevin Brown <kevin@kevin-brown.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL