JavaScript样式/优化:String.indexOf()v。Regex.test() [英] JavaScript style/optimization: String.indexOf() v. Regex.test()

查看:195
本文介绍了JavaScript样式/优化:String.indexOf()v。Regex.test()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看过这段JavaScript程式码:

I've recently come across this piece of JavaScript code:

if (",>=,<=,<>,".indexOf("," + sCompOp + ",") != -1)


$ b b

我很感兴趣,因为写这个测试我会做:

I was intrigued, because to write this test I would have done:

if (/(>=|<=|<>)/.test(sCompOp))

差异,还是其他代码的作者知道一些优化,我不?或者也许有不同的好理由这样做,或者不使用正则表达式...?

Is this just a stylistic difference, or does the author of the other code know something about optimization that I don't? Or perhaps there is a different good reason to do this, or to not use regexes...?

在我看来,使用 String。 indexOf()这是一个更难读的(但是,我很舒服的正则表达式),但有一些实例,它可能比写一个等效的正则表达式更好?

It seems to me that using String.indexOf() for this is a little more difficult to read (but then, I'm quite comfortable with regular expressions), but are there instances where it might be "better" than writing an equivalent regex?

通过更好可能更快或更高效,(虽然显然取决于浏览器的JavaScript引擎),或者一些其他原因我不知道。任何人都可以启发我?

By "better" that might be quicker or more efficient, (although obviously that depends on the browser's JavaScript engine), or some other reason I'm not aware of. Can anyone enlighten me?

推荐答案

我运行了一些测试。第一种方法稍快一点,但即使在大量使用下也不足以产生任何真正的差别... except sCompOp 可能是一个非常长的字符串。因为第一个方法搜索固定长度的字符串,它的执行时间是非常稳定的,无论多长时间 sCompOp 获得,而第二个方法将潜在地遍历整个长度 sCompOp

I ran some tests. The first method is slightly faster, but not by enough to make any real difference even under heavy use... except when sCompOp could potentially be a very long string. Because the first method searches a fixed-length string, its execution time is very stable no matter how long sCompOp gets, while the second method will potentially iterate through the entire length of sCompOp.

此外,第二种方法可能匹配无​​效的字符串 - blah blah blah< = blah blah满足测试...

Also, the second method will potentially match invalid strings - "blah blah blah <= blah blah" satisfies the test...

鉴于您可能在其他地方解析操作,我怀疑边缘情况会是一个问题。但是,即使不是这样,对表达式的小修改将解决这两个问题:

Given that you're likely doing the work of parsing out the operator elsewhere, i doubt either edge case would be a problem. But even if this were not the case, a small modification to the expression would resolve both issues:

/^(>=|<=|<>)$/






< h3>测试代码:


Testing code:

function Time(fn, iter)
{
   var start = new Date();
   for (var i=0; i<iter; ++i)
      fn();
   var end = new Date();
   console.log(fn.toString().replace(/[\r|\n]/g, ' '), "\n : " + (end-start));
}

function IndexMethod(op)
{
   return (",>=,<=,<>,".indexOf("," + op + ",") != -1);
}

function RegexMethod(op)
{
   return /(>=|<=|<>)/.test(op);
}

function timeTests()
{
   var loopCount = 50000;

   Time(function(){IndexMethod(">=");}, loopCount);
   Time(function(){IndexMethod("<=");}, loopCount);
   Time(function(){IndexMethod("<>");}, loopCount);
   Time(function(){IndexMethod("!!");}, loopCount);
   Time(function(){IndexMethod("the quick brown foxes jumped over the lazy dogs");}, loopCount);
   Time(function(){IndexMethod("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");}, loopCount);

   Time(function(){RegexMethod(">=");}, loopCount);
   Time(function(){RegexMethod("<=");}, loopCount);
   Time(function(){RegexMethod("<>");}, loopCount);
   Time(function(){RegexMethod("!!");}, loopCount);
   Time(function(){RegexMethod("the quick brown foxes jumped over the lazy dogs");}, loopCount);
   Time(function(){RegexMethod("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");}, loopCount);
}

timeTests();

在IE6,FF3,Chrome 0.2.149.30中测试

Tested in IE6, FF3, Chrome 0.2.149.30

这篇关于JavaScript样式/优化:String.indexOf()v。Regex.test()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆