Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  133] [ 7]  / answers: 1 / hits: 18698  / 15 Years ago, mon, june 8, 2009, 12:00:00

I'll do some work on a line separated string. Which one will be faster, to split the text via String.split first and then walk on the resultant array or directly walk the whole text via a reg exp and construct the final array on the way?


More From » performance

 Answers
42

Well, the best way to get your answer is to just take 2 minutes and write a loop that does it both ways a thousand times and check firebug to see which one is faster ;)



I've had to optimize a lot of string munging while working on MXHR and in my experience, plain String methods are significantly faster than RegExps in current browsers. Use RegExps on the shortest Strings possible and do everything you possibly can with String methods.



For example, I use this little number in my current code:



var mime = mimeAndPayload.shift().split('Content-Type:', 2)[1].split(;, 1)[0].replace(' ', '');


It's ugly as hell, but believe it or not it's significantly faster than the equivalent RegExp under high load.


[#99353] Thursday, June 4, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
robinh

Total Points: 371
Total Questions: 105
Total Answers: 89

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
robinh questions
Thu, May 6, 21, 00:00, 3 Years ago
Thu, Jan 7, 21, 00:00, 3 Years ago
Fri, Nov 6, 20, 00:00, 4 Years ago
Sun, Aug 9, 20, 00:00, 4 Years ago
;