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
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
23
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
+
24
36
## Avoid Excessive Specificity
25
37
26
38
```
@@ -32,16 +44,15 @@ $( ".data td.gonzalez" );
32
44
33
45
A "flatter" DOM also helps improve selector performance, as the selector engine has fewer layers to traverse when looking for an element.
34
46
35
-
## ID-Based Selectors
36
-
37
-
Beginning your selector with an ID is a safe bet.
47
+
## Save calls to `querySelectorAll`
38
48
39
49
```
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).
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
51
62
When support for older browsers, such as Internet Explorer 8 and below, is necessary, consider the following tips:
52
63
53
64
### Specificity
65
+
54
66
Be specific on the right-hand side of your selector, and less specific on the left.
55
67
56
68
```
@@ -68,9 +80,6 @@ Use `tag.class` if possible on your right-most selector, and just tag or just `.
68
80
Selections that specify or imply that a match could be found anywhere can be very slow.
0 commit comments