New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faster if a little more verbose case insensitive 'on' match #275
Conversation
Order of magnitude+ faster than regexp, see: https://jsfiddle.net/c2h5oh/rr9b6Leb/1/
Indeed! This was actually how it used to work. I switched it out because it made the build a bit smaller, but maybe the runtime performance is more important? Also, I'm not sure how much people care about case-insensitive Thanks for bringing this up though, I really shouldn't have just merged that in without checking with people first! BTW - I had an esbench with roughly the same test as yours, managed to find it: Saves writing the Benchmark.js setup (it's still using that benchmark.js behind the scenes) What do you think? Maybe we go back to case-sensitive, since nobody ever complained about it? 😋 |
I merged and then switched back to the case-sensitive version. You were right about the performance, |
That's how I noticed the change - reading recent commits |
Cool - thanks for this btw, nice to have other people keeping tabs on decisions, haha! |
So I explored this a little bit: added more possible solutions to benchmark. Still using benchmark.js, because esbench made browser throw slow script warnings on a larger number of tests and async just didn't work. https://jsfiddle.net/c2h5oh/84q4ema6/ Using str[0] = 'o' && str[1] as baseline (100%) this is what I got in Chrome 51.0.2704.84:
When I re-run the benchmark in Firefox 48 baseline was roughly 75% faster than Chrome and that was just the start - things got way more interesting after:
Slice is 23+ times faster than char comparison (which is already faster than in chrome) and substring almost 6 times.. |
Thinking Firefox is doing something fancy with short-circuiting the comparison... maybe because the boolean value is never being used? It seems a little bit crazy to me that creating a new String on every iteration would be faster than checking the value at a given offset. Perhaps Regarding esbench - the async checkbox is for an async test, which requires calling |
@c2h5oh hmm - I very slightly modified the tests by setting the outcome of the comparisons to an enclosed variable: https://jsfiddle.net/developit/84q4ema6/6/ With that in place, I'm seeing Chrome3.5x faster than slice (which is the next best) Firefox:24.5x faster than slice (also the next best here) -edit- btw, thanks a ton for digging into this. Helps make sure decisions are rooted in fact! 🎯 |
Chrome: Firefox: |
@c2h5oh is that with the updated jsfiddle? What version of Firefox and what OS? The performance of JS engines varies pretty wildly by OS. |
Yes, updated jsfiddle
|
Hmm. Perhaps this is an OS X oddity then... I'm on OS X 10.10.5 with roughly the same chrome and firefox versions. |
On Windows 10, same computer. Chrome 52.0.2743.116 m Firefox 48.0.1 Internet Explorer 11.1000.14505.0 Edge 39.14905.1000.0 |
Firefox and whatever processor you have are very happy together lol |
Just had a thought: if we wrap the test strings in functions and invoke them on each run with a counter appended, that will disable any crazy caching stuff that might be in play. Here's an updated fiddle to check on Firefox: https://jsfiddle.net/developit/84q4ema6/9/ It's still showing as I would really love it if there was a way to inspect the bytecode Firefox is generating for |
With this I have compare, indexOf, slice and substring within 10% of each other (with compare taking lead) in Chrome A little saner |
See: https://jsfiddle.net/c2h5oh/rr9b6Leb/1/
string.match: 3,418,862 ops/sec ±2.79%
char compare: 38,104,121 ops/sec ±2.67%