forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor-loops.html
More file actions
48 lines (39 loc) · 861 Bytes
/
for-loops.html
File metadata and controls
48 lines (39 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: for loops
Description: optimized for loops
*/
// sub-optimal loop
for (var i = 0; i < myarray.length; i++) {
// do something with myarray[i]
}
for (var i = 0, max = myarray.length; i < max; i++) {
// do something with myarray[i]
}
function looper() {
var i = 0,
max,
myarray = [];
// ...
var i, myarray = [];
for (i = 0, max = myarray.length; i < max; i += 1) {
// do something with myarray[i]
}
for (i = myarray.length; i--;) {
// do something with myarray[i]
}
while (i--) {
// do something with myarray[i]
}
}
// reference
// http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
</script>
</body>
</html>