I am currently doing a big project (by big I mean, many processes) where every millisecond I save means a lot (on the long run), so I want to make sure I am doing it the right way.
So, what is the best way to ensure you will have an array greater than 1?
- a) use indexOf(), then if result is different than -1, split()
- b) split (regardless if characters exist), then do stuff ONLY if the
array.length is greater than 1 - c) another not listed above
Using jsPerf, it appears that omitting
.indexOf()is roughly 23% more efficient that including it over 500,000 iterations (11.67vs.8.95operations per second):Without
indexOf():With
.indexOf():http://jsperf.com/split-and-split-indexof
EDIT
Hmm… If the following line is:
http://jsperf.com/split-and-split-indexof-with-indexof-check
Or any other comparison, it’s seemingly quite a bit faster (by about 69%).
The only reason I can think this is the case is that running
.split()on every variable will perform two functions on each value (find, then separate), instead of just one when necessary. Note, this last part is just a guess.