Skip to content

Commit f367724

Browse files
committed
added documentation to conditionals
1 parent 67ca969 commit f367724

1 file changed

Lines changed: 90 additions & 4 deletions

File tree

general-patterns/conditionals.html

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,106 @@
1111
*/
1212

1313
// NOTE: Paul Irish states that the first statement is only an antipattern when optimizing for
14-
// low-bandwidth source (such as for a bookmarklet.
14+
// low-bandwidth source (such as for a bookmarklet).
1515
// Using the first statement will generally outperform the regex in a loop, and is faster than the
1616
// object literal for lower numbers of conditions (they generally even out around 10 conditions).
1717
// See http://jsperf.com/if-this-or-that
1818

19-
// antipattern
19+
// Normal Pattern
2020
if (type === 'foo' || type === 'bar' ) {}
2121

22-
// preferred method 1 - regex test
22+
23+
/* alternative method 1 - regex test */
2324
if ( /^(foo|bar)$/.test(type) ) {}
2425

25-
// preferred method 2 - object literal lookup (smaller if < 5 items)
26+
27+
/* alternative method 2 - object literal lookup (smaller if < 5 items) */
2628
if ( ({foo:1,bar:1})[type] ) {}
2729

30+
31+
/* alternative method 3 - binary-search-like approach
32+
This approach is best when there are ranges of values for which to test
33+
*/
34+
35+
if (value == 0){
36+
return result0;
37+
} else if (value == 1){
38+
return result1;
39+
} else if (value == 2){
40+
return result2;
41+
} else if (value == 3){
42+
return result3;
43+
} else if (value == 4){
44+
return result4;
45+
} else if (value == 5){
46+
return result5;
47+
} else if (value == 6){
48+
return result6;
49+
} else if (value == 7){
50+
return result7;
51+
} else if (value == 8){
52+
return result8;
53+
} else if (value == 9){
54+
return result9;
55+
} else {
56+
return result10;
57+
}
58+
59+
if (value < 6){
60+
if (value < 3){
61+
if (value == 0){
62+
return result0;
63+
} else if (value == 1){
64+
return result1;
65+
} else {
66+
return result2;
67+
}
68+
} else {
69+
if (value == 3){
70+
return result3;
71+
} else if (value == 4){
72+
return result4;
73+
} else {
74+
return result5;
75+
}
76+
}
77+
} else {
78+
if (value < 8){
79+
if (value == 6){
80+
return result6;
81+
} else {
82+
return result7;
83+
}
84+
} else {
85+
if (value == 8){
86+
return result8;
87+
} else if (value == 9){
88+
return result9;
89+
} else {
90+
return result10;
91+
}
92+
}
93+
}
94+
95+
96+
/* alternative method 4 - object/array lookup tables
97+
Most useful when there is logical mapping between a single key and a single value
98+
*/
99+
100+
if (value == 0) {
101+
return result0;
102+
} else if (value == 1) {
103+
return result1;
104+
} else if (value == 2) {
105+
return result2;
106+
}
107+
108+
// define the array of results
109+
var results = [result0, result1, result2];
110+
// return the correct result
111+
return results[value];
112+
113+
28114
// reference
29115
// http://paulirish.com/2009/perf/
30116
</script>

0 commit comments

Comments
 (0)