forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals.html
More file actions
32 lines (27 loc) · 900 Bytes
/
conditionals.html
File metadata and controls
32 lines (27 loc) · 900 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
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Conditionals
Description: pattern and antipattern of using if else
*/
// NOTE: Paul Irish states that the first statement is only an antipattern when optimizing for
// low-bandwidth source (such as for a bookmarklet.
// Using the first statement will generally outperform the regex in a loop, and is faster than the
// object literal for lower numbers of conditions (they generally even out around 10 conditions).
// See http://jsperf.com/if-this-or-that
// antipattern
if (type === 'foo' || type === 'bar' ) {}
// preferred method 1 - regex test
if ( /^(foo|bar)$/.test(type) ) {}
// preferred method 2 - object literal lookup (smaller if < 5 items)
if ( ({foo:1,bar:1})[type] ) {}
// reference
// http://paulirish.com/2009/perf/
</script>
</body>
</html>