Today i was working on a basic project where I was trying to normalize the data coming from a YAML file, more particularly the Makes and Models of cars.

Before I wanted to let ActiveRecord do anything I first wanted to throw away models that didn't have their corresponding make:

Now I hate slow tests and consequently I hate testing inside of the rails environment. So when my test to ensure that normalize_models went from taking 0.18 seconds to 2.87seconds . It instantly became an unacceptably slow test.

Can you spot my mistake? It's on line 24 of the method make_ids. make_ids is consequently opening a YAML file, parsing it into a ruby hash, converting the hash keys to be my choosing and then collecting the ids. While the code looks clean and is easy to follow it was EXTREMELY slow and could have been dramatically sped up with one thing: Memoization.

After changing this one line my tests sped back up to taking around 0.18 seconds. Adding not even one line of code already saved me 2 seconds each test run. While this might not be critical here just think about your application and where you could be memoizing slow methods

comments powered by Disqus

Mike Silvis