Skip to content

Commit ad5a625

Browse files
Markus Amalthea Magnusonajpiano
Markus Amalthea Magnuson
authored andcommitted
Style and typography fixes and code style adherence in the Performance section. Fixes jquery#334.
1 parent 1765408 commit ad5a625

6 files changed

+60
-79
lines changed

page/performance/append-outside-loop.md

+12-15
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,49 @@ attribution:
66
- jQuery Fundamentals
77
---
88

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.
1110

1211
```
1312
$.each( myArray, function( i, item ) {
1413
15-
var newListItem = "<li>" + item + "</li>";
14+
var newListItem = "<li>" + item + "</li>";
1615
17-
$("#ballers").append( newListItem );
16+
$( "#ballers" ).append( newListItem );
1817
1918
});
2019
```
2120

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.
2522

2623
```
2724
var frag = document.createDocumentFragment();
2825
2926
$.each( myArray, function( i, item ) {
3027
31-
var newListItem = document.createElement("li");
32-
var itemText = document.createTextNode( item );
28+
var newListItem = document.createElement( "li" );
29+
var itemText = document.createTextNode( item );
3330
34-
newListItem.appendChild( itemText );
31+
newListItem.appendChild( itemText );
3532
36-
frag.appendChild( newListItem );
33+
frag.appendChild( newListItem );
3734
3835
});
3936
40-
$("#ballers")[ 0 ].appendChild( frag );
37+
$( "#ballers" )[ 0 ].appendChild( frag );
4138
```
4239

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.
4441

4542
```
4643
var myHtml = "";
4744
4845
$.each( myArray, function( i, item ) {
4946
50-
myHtml += "<li>" + item + "</li>";
47+
myHtml += "<li>" + item + "</li>";
5148
5249
});
5350
54-
$("#ballers").html( myHtml );
51+
$( "#ballers" ).html( myHtml );
5552
```
5653

5754
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.

page/performance/cache-loop-length.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ attribution:
66
- jQuery Fundamentals
77
---
88

9-
In a for loop, don't access the length property of an array every time; cache
10-
it beforehand.
9+
In a for loop, don't access the length property of an array every time; cache it beforehand.
1110

1211
```
1312
var myLength = myArray.length;
1413
1514
for ( var i = 0; i < myLength; i++ ) {
1615
17-
// do stuff
16+
// do stuff
1817
1918
}
2019
```

page/performance/detach-elements-before-work-with-them.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ attribution:
66
- jQuery Fundamentals
77
---
88

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.
1210

1311
```
14-
var $table = $("#myTable");
12+
var $table = $( "#myTable" );
1513
var $parent = $table.parent();
1614
1715
$table.detach();
1816
1917
// ... add lots and lots of rows to table
18+
2019
$parent.append( $table );
2120
```

page/performance/dont-act-on-absent-elements.md

+14-18
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,36 @@ attribution:
66
- jQuery Fundamentals
77
---
88

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.
1210

1311
```
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();
1815
19-
// Better
20-
var $mySelection = $("#nosuchthing");
16+
// Better:
17+
var $mySelection = $( "#nosuchthing" );
2118
2219
if ( $mySelection.length ) {
2320
24-
$mySelection.slideUp();
21+
$mySelection.slideUp();
2522
2623
}
2724
28-
// BEST: add a doOnce plugin
29-
jQuery.fn.doOnce = function( func ){
25+
// Best: Add a doOnce plugin.
26+
jQuery.fn.doOnce = function( func ) {
3027
31-
this.length && func.apply( this );
28+
this.length && func.apply( this );
3229
33-
return this;
30+
return this;
3431
3532
}
3633
37-
$("li.cartitems").doOnce(function() {

34+
$( "li.cartitems" ).doOnce(function() {

3835
39-
// make it ajax! \o/

36+
// make it ajax! \o/

4037
4138
});
4239
```
4340

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.

page/performance/optimize-selectors.md

+23-33
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,56 @@ attribution:
66
- jQuery Fundamentals
77
---
88

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.
1210

1311
## ID-Based Selectors
1412

1513
Beginning your selector with an ID is always best.
1614

1715
```
18-
// fast
19-
$("#container div.robotarm");
16+
// Fast:
17+
$( "#container div.robotarm" );
2018
21-
// super-fast
22-
$("#container").find("div.robotarm");
19+
// Super-fast:
20+
$( "#container" ).find( "div.robotarm" );
2321
```
2422

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.
2924

3025
## Specificity
3126

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.
3428

3529
```
36-
// unoptimized
37-
$("div.data .gonzalez");
38-
39-
// optimized
40-
$(".data td.gonzalez");
30+
// Unoptimized:
31+
$( "div.data .gonzalez" );
4132
33+
// Optimized:
34+
$( ".data td.gonzalez" );
4235
```
4336

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.
4638

47-
## Avoid excessive specificity.
39+
## Avoid Excessive Specificity
4840

4941
```
50-
$(".data table.attendees td.gonzalez");
42+
$( ".data table.attendees td.gonzalez" );
5143
52-
// better: drop the middle if possible
53-
$(".data td.gonzalez");
44+
// Better: Drop the middle if possible.
45+
$( ".data td.gonzalez" );
5446
```
5547

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.
5849

5950
## Avoid the Universal Selector
6051

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.
6353

6454
```
65-
$(".buttons > *"); // extremely expensive
66-
$(".buttons").children(); // much better
55+
$( ".buttons > *" ); // Extremely expensive.
56+
$( ".buttons" ).children(); // Much better.
6757
68-
$(".gender :radio"); // implied universal selection
69-
$(".gender *:radio"); // same thing, explicit now
70-
$(".gender input:radio"); // much better
58+
$( ".category :radio" ); // Implied universal selection.
59+
$( ".category *:radio" ); // Same thing, explicit now.
60+
$( ".category input:radio" ); // Much better.
7161
```

page/performance/use-stylesheets-for-changing-css.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ attribution:
66
- jQuery Fundamentals
77
---
88

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.
1110

1211
```
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" );
1514
16-
$("<style type=\"text/css\">a.swedberg { color : #asd123 }</style>")
17-
.appendTo("head");
15+
// Much faster:
16+
$( "<style type=\"text/css\">a.swedberg { color: #0769ad }</style>")
17+
.appendTo( "head" );
1818
```

0 commit comments

Comments
 (0)