Jquery selector performance

Is there a more efficient way to write this.

$('#test').find('option:selected[value!=""]')
+3
source share
2 answers

You can configure it a bit, but using methods instead of Sizzle:

$('#test').find('option').filter(function() {
    return this.selected && this.value.length
});

Benchmark : http://jsperf.com/sizzle-vs-methods-filter/12

.filter() for me, about 70%.

+5
source

Well, there will always be only one selected, therefore, in my opinion, you do not need a handler find().

I just write like this:

$('#test option:selected[value!=""]')

I have not tested it yet.

0
source

Source: https://habr.com/ru/post/1790643/


All Articles