Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  184] [ 7]  / answers: 1 / hits: 73760  / 14 Years ago, fri, january 21, 2011, 12:00:00

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


str.indexOf("src") 

and


str.match(/src/)

I personally prefer match (and regexp) but colleagues seem to go the other way. We were wondering if it mattered ...?


EDIT:


I should have said at the outset that this is for functions that will be doing partial plain-string matching (to pick up identifiers in class attributes for JQuery) rather than full regexp searches with wildcards etc.


class='redBorder DisablesGuiClass-2345-2d73-83hf-8293' 

So it's the difference between:


string.indexOf('DisablesGuiClass-');

and


string.match(/DisablesGuiClass-/)

More From » regex

 Answers
37

RegExp is indeed slower than indexOf (you can see it here), though normally this shouldn't be an issue. With RegExp, you also have to make sure the string is properly escaped, which is an extra thing to think about.



Both of those issues aside, if two tools do exactly what you need them to, why not choose the simpler one?


[#94110] Wednesday, January 19, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsley

Total Points: 352
Total Questions: 84
Total Answers: 94

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;