var a = 1, b = 2, c = 3; if (a == 1) { if (b == 2) { if (c == 3) { ... } } }
if
:if (a == 1 && b == 2 && c == 3) { ... }
if-then-else
expression. But you need to keep in mind that the performance of such structures:var v = a == 1? b == 2? c == 3? 1: 0: 0: 0;
if (a + b + c == 6) { ... }
undefined
, not NaN
, not null
, not ''
and not 0), then the next option will work 5–10% faster, than the previous case (and 10–20% faster than the very first example):if (a && b && c) { ... }
var a = 1, b = 2, c = '3'; if (a == 1 && b == 2 && c === '3') { ... }
===
, which in the case of non-numeric variables is 10–20% faster than the usual comparison.RegExp
object ( exec
, test
) or the string methods ( match
, search
, indexOf
). If we just need to check if the string matches a regular expression, then the test
best suited for this:var str = 'abc', regexp = new RegExp ('abc'); if (regexp.test (str)) { ... }
exec
:if (regexp.exec (str) [1]) { ... }
match
method is similar to the exec
method of the RegExp
object being created, but it works 10–15% faster in the case of simple expressions. However, the search
method is slightly slower (5–10%) than test
, because the latter does not return the found substring.if (/abc/.test (str)) { ... }
indexOf
, which works 2 times faster than regular expressions parsing:if (str.indexOf ('abc')! = -1) { ... }
/a|b|c/
. In this case, we need to check in the given string whether one of the possible options is present (or if the string is equal to this variant). In the case of an exact match, a regular expression faster (by 50%) will check the string as the key of a hash:var hash = {'a': 1, 'b': 1}, str = 'a'; if (h [str]) { ... }
if (str === 'a' || str === 'b') { ... }
if
, switch
with the appropriate values ​​and checking the values ​​in the hash, then the following interesting feature should be noted. With a small if
nesting level (if there are only a few values, or we very often exit the first or second value), the if
and switch
constructions overtake the hash by about 10%. If we have a lot of values, and they are all approximately equiprobable, then the hash works out in the general case already by 20% faster. This applies equally to setting variables, as well as calling functions. T, e. To create a branch with a function call, it is best to use a hash.test
:if (/^[\w[:#.[[\w\RL**||=====$/.test (selector)) { ... } else { ... }
switch
used:switch (firstLetter) { case '#': ... break; case '.': ... break; case ':' ... break; case '[': ... break; default: ... break; }
>
, +
, ~
,): here, too, just switch
:switch (ancestor) { case '': ... break; case '~': ... break; case '+': ... break; case '>': ... break; }
first-child
, last-child
, nth-child
, etc.) and the choice of check function for attributes ( ~=
, *=
, =
, etc.) is made already through special hashes:_.attr = {'': ..., '=': ..., '& =': ..., '^ =': ... ...}
Task | Solution tool |
---|---|
Numeric value check | Normal comparison ( == ) |
Check multiple numeric values | Comparing their amount |
Check that the number is not zero, or check for existence | Checking the negation of a given variable ( ! ) |
Parsing a string and selecting parts into an array | String.match(RegExp) or RegExp.exec(String) |
Regular expression matching string | RegExp.test(String) |
Check string for the presence of a substring | String.indexOf(String) |
Check string for exact match (or match one of a set of values) | if without a cast ( === ) |
Selection depending on the exact value (values ​​1–2) | Conditional if construction |
Selection depending on the exact value (values ​​3–8) | switch |
Selection depending on the exact value (values ​​greater than 8) | Hash with keys corresponding to values |
Source: https://habr.com/ru/post/50210/
All Articles