Skip to content

Conversation

@vyshkant
Copy link
Contributor

@vyshkant vyshkant commented Feb 16, 2020

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.

@vyshkant
Copy link
Contributor Author

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

@kevin-brown
Copy link
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.

@vyshkant
Copy link
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?)

@vyshkant
Copy link
Contributor Author

And when I open QUnit in browser I can see tests are passed:
screenshot-localhost_9999-2020 02 24-02_43_20


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

Choose a reason for hiding this comment

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

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

data.push(option);
});
for (var index in selectedElements) {
if (selectedElements.hasOwnProperty(index)) {
Copy link
Member

Choose a reason for hiding this comment

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

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.

@vyshkant
Copy link
Contributor Author

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

@vyshkant vyshkant requested a review from kevin-brown February 25, 2020 05:00
Copy link
Member

@kevin-brown kevin-brown left a comment

Choose a reason for hiding this comment

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

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.

@vyshkant vyshkant requested a review from kevin-brown March 8, 2020 23:47
@vyshkant
Copy link
Contributor Author

vyshkant commented Mar 8, 2020

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

@kevin-brown
Copy link
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 kevin-brown merged commit cbadb0a into select2:develop Apr 19, 2020
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