[removed] indexOf vs. Match when Searching Strings?

前端 未结 9 1563
感情败类
感情败类 2020-12-04 13:16

Readability aside, are there any discernable differences (performance perhaps) between using

str.indexOf("src") 

and



        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 13:33

    Using indexOf should, in theory, be faster than a regex when you're just searching for some plain text, but you should do some comparative benchmarks yourself if you're concerned about performance.

    If you prefer match and it's fast enough for your needs then go for it.

    For what it's worth, I agree with your colleagues on this: I'd use indexOf when searching for a plain string, and use match etc only when I need the extra functionality provided by regular expressions.

提交回复
热议问题