18

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?

3 Answers 3

22

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I considered testing via Firebug but actually didn't trust the time measurement of JS. Thank you very much for your answer with a supporting example ;)
So don't trust firebug - create and compare a couple of date objects as well
Well I do not trust the JS Date objects itself since when(some time ago) I tried to measure performance, although it claimed it was milliseconds precise, it was always returning multiples of seconds. This is why I do not trust it.
Note also that different implementations of JavaScript (Firefox, Internet Explorer, WebKit, etc.. ) will have different perforamnces.
I know this question is old, but for other people's sakes. The performance object performs way better than the Date object for timing. Still gives some bogus data (like zero when it obviously wasn't zero) so you can filter those out. I have found this to be the easiest way to narrow down speeds of specific implementation choices.
|
14

While this is 2½ years late, hopefully this helps shed some light on the matter for any future viewers: https://jsperf.app/split-join-vs-regex-replace (Includes benchmarks results for multiple browsers, as well the functional benchmark code itself)

2 Comments

Ran it twice, in the first test, split won and in the second test, regex won.
@Andrew Odri - Link seems broken! Please update! :-)
2

I expect that using split() will be much faster. It depends upon many specifics, number of lines vs. length, complexity of regex, etc.

5 Comments

Thanks for taking time to answer. I also expected the native methods to be faster but nothing can be better than an experience for this question IMO. Thanks anyway =)
That answer IS based on experience :) as dfltr said, you need to experiment for your particular case.
Well the problem is, I do not know the string size and the regexp will be a simple \n|\r matcher if I use regexp. Any more suggestions? =) (BTW sorry for thinking it was an estimate ;))
I've never found .split() to be faster than regex.
@KevinBeal Not the exact same code as OP, but try this as an example: measurethat.net/Benchmarks/Show/7822/0/strmatch-vs-strsplit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.