Skip to content

Commit cb2c81b

Browse files
committed
optimize-selectors qSA before Sizzle
1 parent 9878874 commit cb2c81b

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

page/performance/optimize-selectors.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ $( "#my-table tr:nth-child(odd)" );
2121

2222
Keep in mind that many jQuery extensions, including `:even` in the above example, do not have exact equivalents in the CSS specification. In some situations the convenience of these extensions could outweigh their performance cost.
2323

24+
## Try to segregate the nonstandard parts of your selection
25+
26+
When you select elements, jQuery will call `querySelectorAll` with your selection. If `querySelectorAll` throws an error, jQuery will refer to its Sizzle engine. So, if you are using at least one of the nonstandard pseudo-classes such as `:contains()`, `:has`, `:even`, `:submit`, etc. You will not take advantage of the native `querySelectorAll`.
27+
28+
```
29+
// A long selection with nonstandard pseudo-classes inside
30+
$( "#global.ready .part .list li a:contains('qwerty'):first" );
31+
32+
// A long standard selection with a filter outside (faster):
33+
$( "#global.ready .part .list li a").filter( ":contains('qwerty'):first" );
34+
```
35+
2436
## Avoid Excessive Specificity
2537

2638
```
@@ -32,16 +44,15 @@ $( ".data td.gonzalez" );
3244

3345
A "flatter" DOM also helps improve selector performance, as the selector engine has fewer layers to traverse when looking for an element.
3446

35-
## ID-Based Selectors
36-
37-
Beginning your selector with an ID is a safe bet.
47+
## Save calls to `querySelectorAll`
3848

3949
```
40-
// Fast:
41-
$( "#container div.robotarm" );
50+
// If in your HTML there are 2 .container with 5 div in each,
51+
// this line will call querySelectorAll 13 times (1 + 2 + 2*5).
52+
$( ".container" ).children( "div" ).find( ".robotarm" );
4253
43-
// Super-fast:
44-
$( "#container" ).find( "div.robotarm" );
54+
// Against only 1 call with this:
55+
$( ".container div .robotarm" );
4556
```
4657

4758
With the first approach, jQuery queries the DOM using `document.querySelectorAll()`. With the second, jQuery uses `document.getElementById()`, which is faster, although the speed improvement may be diminished by the subsequent call to `.find()`.
@@ -51,6 +62,7 @@ With the first approach, jQuery queries the DOM using `document.querySelectorAll
5162
When support for older browsers, such as Internet Explorer 8 and below, is necessary, consider the following tips:
5263

5364
### Specificity
65+
5466
Be specific on the right-hand side of your selector, and less specific on the left.
5567

5668
```
@@ -68,9 +80,6 @@ Use `tag.class` if possible on your right-most selector, and just tag or just `.
6880
Selections that specify or imply that a match could be found anywhere can be very slow.
6981

7082
```
71-
$( ".buttons > *" ); // Extremely expensive.
72-
$( ".buttons" ).children(); // Much better.
73-
7483
$( ":radio" ); // Implied universal selection.
7584
$( "*:radio" ); // Same thing, explicit now.
7685
$( "input:radio" ); // Much better.

0 commit comments

Comments
 (0)