D
D
darkside2012-04-30 21:47:21
JavaScript
darkside, 2012-04-30 21:47:21

And what is easier for JS - a simple condition or a regexp?

For example, (x == 'a' || x == 'b' || x == 'c') or x.match(/^(?:a|b|c)$/)?

Answer the question

In order to leave comments, you need to log in

10 answer(s)
S
SerDIDG, 2012-04-30
@SerDIDG

In this case, it would be more economical:

/^(a|b|c)$/.test(x);

K
krasulya, 2012-04-30
@krasulya

Made some measurements in chrome. On average, the first option (10,000 normal comparisons) takes 30-45 ms. The second is 20-25. If it is equal to , then each time it will be checked only and then the first option passes in 25-35 ms. Those. in this particular example, regexps win. But in other cases, the result may be different.
var x = '1',
times = 10000,
start1 = +new Date();
for (var i=0; i<times; i++) {
if (x == 'a' || x == 'b' || x == 'c') {
}
}
var time1 = +new Date() - start1,
start2 = +new Date();
for (var i=0; i<times; i++) {
if (x.match(/^(?:a|b|c)$/)) {
}
}
var time2 = +new Date() - start2;
console.log(time1, time2)

M
mark_ablov, 2012-05-01
@mark_ablov

Your tests and results are strange.
jsperf.com/if-vs-regexp-match
Of course if is an order of magnitude faster.

M
Meliborn, 2012-04-30
@Meliborn

A regular expression is a function call, which is a more expensive process.

[
[email protected]><e, 2012-04-30
@barmaley_exe

What does "easier" mean? Runs faster? Of course, the first, because regexps must first be compiled, then work with them ... This is clearly more complicated than just comparing lines.
In general, it seems to me that this is some kind of savings on matches. Why is the performance of this code so important to you?

N
neosys, 2012-05-01
@neosys

It is better to use the usual conditions. Yes, you can win 1-10ms with regexps, but it will make your code so badly readable that it makes you wonder if the game is worth the candle.

G
gro, 2012-05-02
@gro

Using "===" instead of "==" will allow the first option to win even more convincingly in this pointless competition (in FF at least).

P
Pavlo Ponomarenko, 2012-05-02
@TheShock

I would use something like this:

if (possibleValues.indexOf(item)) >= 0

The question is extremely stupid. Write in the way that is easiest to maintain and fastest to expand, and if necessary go through the profiler and look for bottlenecks.

K
Keenest, 2012-05-09
@Keenest

in this case it doesn't matter. unless you have such checks performed so often that it affects performance.
with if'ami it is a little more evident, but in some situations without regular expressions it will be heavy.
for example, I use a function to check if a string is a valid e-mail:

return /^[0-9a-z_\.][email protected][0-9a-z_\^\.]+\.[a-z]{2,6}$/i.test(s);

if's would have turned out oh how hard)

X
XimikS, 2012-05-01
@XimikS

well, in modern browser engines, regexps compile, and after that they are very fast, so if you need to use somewhere in the loop, regexp is better =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question