You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/performance/optimize-selectors.md
+36-19
Original file line number
Diff line number
Diff line change
@@ -5,11 +5,36 @@
5
5
"attribution": [ "jQuery Fundamentals" ]
6
6
}</script>
7
7
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.
9
34
10
35
## ID-Based Selectors
11
36
12
-
Beginning your selector with an ID is always best.
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
23
50
24
-
## Specificity
51
+
When support for older browsers, such as Internet Explorer 8 and below, is necessary, consider the following tips:
25
52
53
+
### Specificity
26
54
Be specific on the right-hand side of your selector, and less specific on the left.
27
55
28
56
```
@@ -35,26 +63,15 @@ $( ".data td.gonzalez" );
35
63
36
64
Use `tag.class` if possible on your right-most selector, and just tag or just `.class` on the left.
37
65
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
50
67
51
68
Selections that specify or imply that a match could be found anywhere can be very slow.
0 commit comments