Skip to content

Commit 82c7503

Browse files
committed
Improve/update optimize-selectors page. Fixes jquerygh-697. Closes jquerygh-704
1 parent d08015a commit 82c7503

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

page/performance/optimize-selectors.md

+36-19
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,36 @@
55
"attribution": [ "jQuery Fundamentals" ]
66
}</script>
77

8-
Selector optimization is less important than it used to be, as more browsers implement `document.querySelectorAll()` and the burden of selection shifts from jQuery to the browser. However, there are still some tips to keep in mind.
8+
Selector optimization is less important than it used to be, as more browsers implement `document.querySelectorAll()` and the burden of selection shifts from jQuery to the browser. However, there are still some tips to keep in mind when selector performance becomes a bottleneck.
9+
10+
## jQuery Extensions
11+
12+
When possible, avoid selectors that include [jQuery extensions](https://api.jquery.com/category/selectors/jquery-selector-extensions/). These extensions cannot take advantage of the performance boost provided by the native `querySelectorAll()` DOM method and, therefore, require the use of the Sizzle selector engine provided by jQuery.
13+
14+
```
15+
// Slower (the zero-based :even selector is a jQuery extension)
16+
$( "#my-table tr:even" );
17+
18+
// Better, though not exactly equivalent
19+
$( "#my-table tr:nth-child(odd)" );
20+
```
21+
22+
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.
23+
24+
## Avoid Excessive Specificity
25+
26+
```
27+
$( ".data table.attendees td.gonzalez" );
28+
29+
// Better: Drop the middle if possible.
30+
$( ".data td.gonzalez" );
31+
```
32+
33+
A "flatter" DOM also helps improve selector performance, as the selector engine has fewer layers to traverse when looking for an element.
934

1035
## ID-Based Selectors
1136

12-
Beginning your selector with an ID is always best.
37+
Beginning your selector with an ID is a safe bet.
1338

1439
```
1540
// Fast:
@@ -19,10 +44,13 @@ $( "#container div.robotarm" );
1944
$( "#container" ).find( "div.robotarm" );
2045
```
2146

22-
The `.find()` approach is faster because the first selection is handled without going through the Sizzle selector engine – ID-only selections are handled using `document.getElementById()`, which is extremely fast because it is native to the browser.
47+
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()`.
48+
49+
## Tips for Older Browsers
2350

24-
## Specificity
51+
When support for older browsers, such as Internet Explorer 8 and below, is necessary, consider the following tips:
2552

53+
### Specificity
2654
Be specific on the right-hand side of your selector, and less specific on the left.
2755

2856
```
@@ -35,26 +63,15 @@ $( ".data td.gonzalez" );
3563

3664
Use `tag.class` if possible on your right-most selector, and just tag or just `.class` on the left.
3765

38-
## Avoid Excessive Specificity
39-
40-
```
41-
$( ".data table.attendees td.gonzalez" );
42-
43-
// Better: Drop the middle if possible.
44-
$( ".data td.gonzalez" );
45-
```
46-
47-
A "flatter" DOM also helps improve selector performance, as the selector engine has fewer layers to traverse when looking for an element.
48-
49-
## Avoid the Universal Selector
66+
### Avoid the Universal Selector
5067

5168
Selections that specify or imply that a match could be found anywhere can be very slow.
5269

5370
```
5471
$( ".buttons > *" ); // Extremely expensive.
5572
$( ".buttons" ).children(); // Much better.
5673
57-
$( ".category :radio" ); // Implied universal selection.
58-
$( ".category *:radio" ); // Same thing, explicit now.
59-
$( ".category input:radio" ); // Much better.
74+
$( ":radio" ); // Implied universal selection.
75+
$( "*:radio" ); // Same thing, explicit now.
76+
$( "input:radio" ); // Much better.
6077
```

0 commit comments

Comments
 (0)