|
11 | 11 | */ |
12 | 12 |
|
13 | 13 | // 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). |
15 | 15 | // Using the first statement will generally outperform the regex in a loop, and is faster than the |
16 | 16 | // object literal for lower numbers of conditions (they generally even out around 10 conditions). |
17 | 17 | // See http://jsperf.com/if-this-or-that |
18 | 18 |
|
19 | | -// antipattern |
| 19 | +// Normal Pattern |
20 | 20 | if (type === 'foo' || type === 'bar' ) {} |
21 | 21 |
|
22 | | -// preferred method 1 - regex test |
| 22 | + |
| 23 | +/* alternative method 1 - regex test */ |
23 | 24 | if ( /^(foo|bar)$/.test(type) ) {} |
24 | 25 |
|
25 | | -// preferred method 2 - object literal lookup (smaller if < 5 items) |
| 26 | + |
| 27 | +/* alternative method 2 - object literal lookup (smaller if < 5 items) */ |
26 | 28 | if ( ({foo:1,bar:1})[type] ) {} |
27 | 29 |
|
| 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 | + |
28 | 114 | // reference |
29 | 115 | // http://paulirish.com/2009/perf/ |
30 | 116 | </script> |
|
0 commit comments