joyent/node — pull request #3267 by FB55 (2012-05-14 19:07:33+00:00)
Title: Some speed improvements to querystring

I couldn't really understand [this commit message](https://github.com/joyent/node/commit/5e3ca981556c305830553d006b76d5dcaaf49276) _(Make QueryString.parse() even faster)_, as there was some crappy code in the module. So I tried to make it a true statement, optimizing at different places.

Some of the things that were optimized:
* __Avoid switch statements__: At one place, there was a switch statement that just checked for a single condition. Extremely wasteful. Besides, if-else constructs are much more readable.
* __Don't cache typeof__: A couple of days ago, I found [this test](http://jsperf.com/cached-typeof/) at jsPerf, which is pretty interesting. It looks like V8 optimizes `typeof x === 'type'` calls, so caching the call is actually bad. I created a [fork](http://jsperf.com/cached-typeof/2) including the switch statement, have a look at your own (a hint: Only half of the iterations with switch). This isn't critical, but it's also only a small change.
* __Escape from the `charCode()` hell__: I don't know why there were so many (unnecessary) calls of `charCode`, but string comparison is faster by several magnitudes, so let's use that. Besides, all the calls could be replaced with hardcoded numbers (which, of course, would hurt readability, so I didn't do that).
*__Don't assign things to themselves__: `foo = foo || 'bar'` is a pretty common pattern, but an `if` is the better choice in performance sensitive code. (I used an `if` that's directly followed by the assignment, I hope that fits nodes style.)

I guess I need to sign something before gettings this merged, so point me at it and I'll do it. And when there's anything else to do before getting this merged, tell me.