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/append-outside-loop.md
+12-15
Original file line number
Diff line number
Diff line change
@@ -6,52 +6,49 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
Touching the DOM comes at a cost; if you're appending a lot of elements to the
10
-
DOM, you will want to append them all at once, rather then one at a time. This is common problem when appending elements within a loop.
9
+
Touching the DOM comes at a cost; if you're appending a lot of elements to the DOM, you will want to append them all at once, rather then one at a time. This is common problem when appending elements within a loop.
11
10
12
11
```
13
12
$.each( myArray, function( i, item ) {
14
13
15
-
var newListItem = "<li>" + item + "</li>";
14
+
var newListItem = "<li>" + item + "</li>";
16
15
17
-
$("#ballers").append( newListItem );
16
+
$( "#ballers").append( newListItem );
18
17
19
18
});
20
19
```
21
20
22
-
One common technique is to leverage a document fragment. During each iteration
23
-
of the loop, you append the element to the fragment rather than the DOM
24
-
element. After the loop, just append the fragment to the DOM element.
21
+
One common technique is to leverage a document fragment. During each iteration of the loop, you append the element to the fragment rather than the DOM element. After the loop, just append the fragment to the DOM element.
25
22
26
23
```
27
24
var frag = document.createDocumentFragment();
28
25
29
26
$.each( myArray, function( i, item ) {
30
27
31
-
var newListItem = document.createElement("li");
32
-
var itemText = document.createTextNode( item );
28
+
var newListItem = document.createElement("li");
29
+
var itemText = document.createTextNode( item );
33
30
34
-
newListItem.appendChild( itemText );
31
+
newListItem.appendChild( itemText );
35
32
36
-
frag.appendChild( newListItem );
33
+
frag.appendChild( newListItem );
37
34
38
35
});
39
36
40
-
$("#ballers")[ 0 ].appendChild( frag );
37
+
$("#ballers")[ 0 ].appendChild( frag );
41
38
```
42
39
43
-
Another technique, which is quite simple, is to build up a string during each iteration of the loop. After the loop, just set the html of the DOM element to that string.
40
+
Another technique, which is quite simple, is to build up a string during each iteration of the loop. After the loop, just set the HTML of the DOM element to that string.
44
41
45
42
```
46
43
var myHtml = "";
47
44
48
45
$.each( myArray, function( i, item ) {
49
46
50
-
myHtml += "<li>" + item + "</li>";
47
+
myHtml += "<li>" + item + "</li>";
51
48
52
49
});
53
50
54
-
$("#ballers").html( myHtml );
51
+
$("#ballers").html( myHtml );
55
52
```
56
53
57
54
There are of course other techniques you could certainly test out; a great way to test the performance of these is through a site called [jsperf](http://jsperf.com). This site allows you to benchmark each technique and visually see how it performs across all the browsers.
Copy file name to clipboardExpand all lines: page/performance/detach-elements-before-work-with-them.md
+3-4
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,15 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
The DOM is slow; you want to avoid manipulating it as much as possible. jQuery
10
-
introduced `.detach()` in version 1.4 to help address this issue, allowing you
11
-
to remove an element from the DOM while you work with it.
9
+
The DOM is slow; you want to avoid manipulating it as much as possible. jQuery introduced `detach()` in version 1.4 to help address this issue, allowing you to remove an element from the DOM while you work with it.
Copy file name to clipboardExpand all lines: page/performance/dont-act-on-absent-elements.md
+14-18
Original file line number
Diff line number
Diff line change
@@ -6,40 +6,36 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
jQuery won't tell you if you're trying to run a whole lot of code on an empty
10
-
selection — it will proceed as though nothing's wrong. It's up to you to verify
11
-
that your selection contains some elements.
9
+
jQuery won't tell you if you're trying to run a whole lot of code on an empty selection – it will proceed as though nothing's wrong. It's up to you to verify that your selection contains some elements.
12
10
13
11
```
14
-
// BAD: this runs three functions
15
-
// before it realizes there's nothing
16
-
// in the selection
17
-
$("#nosuchthing").slideUp();
12
+
// Bad: This runs three functions before it
13
+
// realizes there's nothing in the selection
14
+
$( "#nosuchthing" ).slideUp();
18
15
19
-
// Better
20
-
var $mySelection = $("#nosuchthing");
16
+
// Better:
17
+
var $mySelection = $("#nosuchthing");
21
18
22
19
if ( $mySelection.length ) {
23
20
24
-
$mySelection.slideUp();
21
+
$mySelection.slideUp();
25
22
26
23
}
27
24
28
-
// BEST: add a doOnce plugin
29
-
jQuery.fn.doOnce = function( func ){
25
+
// Best: Add a doOnce plugin.
26
+
jQuery.fn.doOnce = function( func ){
30
27
31
-
this.length && func.apply( this );
28
+
this.length && func.apply( this );
32
29
33
-
return this;
30
+
return this;
34
31
35
32
}
36
33
37
-
$("li.cartitems").doOnce(function() {
34
+
$("li.cartitems").doOnce(function() {
38
35
39
-
// make it ajax! \o/
36
+
// make it ajax! \o/
40
37
41
38
});
42
39
```
43
40
44
-
This guidance is especially applicable for jQuery UI widgets, which have a lot
45
-
of overhead even when the selection doesn't contain elements.
41
+
This guidance is especially applicable for jQuery UI widgets, which have a lot of overhead even when the selection doesn't contain elements.
Copy file name to clipboardExpand all lines: page/performance/optimize-selectors.md
+23-33
Original file line number
Diff line number
Diff line change
@@ -6,66 +6,56 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
Selector optimization is less important than it used to be, as more browsers
10
-
implement document.querySelectorAll() and the burden of selection shifts from
11
-
jQuery to the browser. However, there are still some tips to keep in mind.
9
+
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.
12
10
13
11
## ID-Based Selectors
14
12
15
13
Beginning your selector with an ID is always best.
16
14
17
15
```
18
-
// fast
19
-
$("#container div.robotarm");
16
+
// Fast:
17
+
$("#container div.robotarm");
20
18
21
-
// super-fast
22
-
$("#container").find("div.robotarm");
19
+
// Super-fast:
20
+
$("#container").find("div.robotarm");
23
21
```
24
22
25
-
The `.find()` approach is faster because the first selection is handled
26
-
without going through the Sizzle selector engine — ID-only selections are
27
-
handled using `document.getElementById()`, which is extremely fast because it is
28
-
native to the browser.
23
+
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.
29
24
30
25
## Specificity
31
26
32
-
Be specific on the right-hand side of your selector, and less specific on the
33
-
left.
27
+
Be specific on the right-hand side of your selector, and less specific on the left.
34
28
35
29
```
36
-
// unoptimized
37
-
$("div.data .gonzalez");
38
-
39
-
// optimized
40
-
$(".data td.gonzalez");
30
+
// Unoptimized:
31
+
$( "div.data .gonzalez" );
41
32
33
+
// Optimized:
34
+
$( ".data td.gonzalez" );
42
35
```
43
36
44
-
Use `tag.class` if possible on your right-most selector, and just tag or just
45
-
`.class` on the left.
37
+
Use `tag.class` if possible on your right-most selector, and just tag or just `.class` on the left.
46
38
47
-
## Avoid excessive specificity.
39
+
## Avoid Excessive Specificity
48
40
49
41
```
50
-
$(".data table.attendees td.gonzalez");
42
+
$(".data table.attendees td.gonzalez");
51
43
52
-
// better: drop the middle if possible
53
-
$(".data td.gonzalez");
44
+
// Better: Drop the middle if possible.
45
+
$(".data td.gonzalez");
54
46
```
55
47
56
-
A "flatter" DOM also helps improve selector performance, as the selector engine
57
-
has fewer layers to traverse when looking for an element.
48
+
A "flatter" DOM also helps improve selector performance, as the selector engine has fewer layers to traverse when looking for an element.
58
49
59
50
## Avoid the Universal Selector
60
51
61
-
Selections that specify or imply that a match could be found anywhere can be
62
-
very slow.
52
+
Selections that specify or imply that a match could be found anywhere can be very slow.
Copy file name to clipboardExpand all lines: page/performance/use-stylesheets-for-changing-css.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -6,13 +6,13 @@ attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
9
-
If you're changing the CSS of more than 20 elements using `.css()`, consider
10
-
adding a style tag to the page instead for a nearly 60% increase in speed.
9
+
If you're changing the CSS of more than 20 elements using `.css()`, consider adding a style tag to the page instead for a nearly 60% increase in speed.
11
10
12
11
```
13
-
// fine for up to 20 elements, slow after that
14
-
$("a.swedberg").css("color", "#asd123");
12
+
// Fine for up to 20 elements, slow after that:
13
+
$("a.swedberg").css("color", "#0769ad" );
15
14
16
-
$("<style type=\"text/css\">a.swedberg { color : #asd123 }</style>")
0 commit comments