Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

improve inefficient jQuery selector

In IntelliJ, if I use a jQuery selector such as:

$('#roleField option').each(function() {
    // impl omitted
});

The selector is highlighted with a suggestion that I should

split descendant selectors which are prefaced with ID selector

what exactly is IntelliJ suggesting that I should replace the selector above with?

like image 218
Dónal Avatar asked May 07 '14 12:05

Dónal


People also ask

Which jQuery selector is fastest?

ID and Element selector are the fastest selectors in jQuery.

Which selector is faster in JavaScript?

Ofceauce ID is a faster selector in both CSS and JavaScript.

Is jQuery slower than JavaScript?

A. When it comes to speed, jQuery is quite fast for modern browsers on modern computers. So is pure JavaScript but both run very slow on older browsers and machines. Pure Javascript functions will be faster than jQuery operations.


1 Answers

From the jquery documentation this method will not go through the Sizzle sector engine:

$('#roleField option').each(function() {
    // No Sizzle
}); 

Where this one will:

$('#roleField').find('option')  // Sizzle!

Look at the ID base selectors section here. Hence the second method will be faster than the first.

like image 184
terpinmd Avatar answered Oct 10 '22 01:10

terpinmd