k33l0r/radix62 — pull request #1 by sfcgeorge (2013-04-23 16:57:38+00:00)
Title: Sped up encode62 and decode62

I have refactored the code to speed up the encode62 and decode62 methods. My benchmarks show encode62 is 20% faster and decode62 is 100% faster. Their functionality stays the same so all the tests still pass. I have also added a few tests where relevant. I will explain the changes:

### Encode62

* Changed the order of the if statement so that the most common choice comes fast and the least common last. This means less time is spent performing comparisons in most cases.
* Switched `.modulo()` method to `%` modulo operator as this is faster. Not sure why, perhaps an extra method call.

### Decode62

* Added `ALPHABET_HASH` constant so that we can look up the number of each letter by key rather than using `.find_index()` which is slower.
* Changed the algorithm so that we aren't calculating a power within the loop, we can use `.each` rather than `.each_with_index` and removed the `.reverse` as the results come in the right order now already.
* Removed the Regex as this was slow. The same error checking is achieved by using `.find()` on the hash which raised an exception if the key is not found. We then catch that exception to return our own more descriptive exception from before.

This is a very minor pull request for a not huge speed increase, but I think it is still worth it, because a few milliseconds here and there could speed up a URL shortener. I love the gem, it's nice and simple. Thanks!