4 ms·
Someone already set up a jsperf test for this an hour or so ago, so I added my regex solution. http://jsperf.com/spacify/2 http://jsperf.com/spacify/2 On my m
by parksy 13y ago
Someone already set up a jsperf test for this an hour or so ago, so I added my regex solution. http://jsperf.com/spacify/2 http://jsperf.com/spacify/2
On my machine, I was surprised to see that the for loop is the fastest (Chrome + Crunchbang).
I'd guess that this is probably because looping structures are heavily optimized in modern JS engines.
- bulatb 13y agoOne thing I've learned from jsPerf is that this sort of micro-tuning tends to be particularly fruitless in JavaScript. Even if a tweak increases speed consistently across the target browsers, there's no telling which will optimize what cases in the next few versions. Basic things like caching DOM lookups are always good, but not so much the little things.
- parksy 13y agoYup, I just ran the tests again and the results are completely different and what difference there is is basically negligible.