Let’s talk about performance optimization.
What comes to mind when you think of this phrase? By default, we tend to think about making code run faster. When and how should we make code run faster? Let’s look at an example to gain some intuition.
Suppose we have an app that helps us sort, analyze, and aggregate the information in mathematics textbooks. Inside this app we break down the textbooks by unit, chapter, section, topic, and formula. We want to make a page in this app for each of the topics in the textbook, and we want to make a sidebar on this page that shows all of the formulas that might be valuable to know while reviewing this topic. So to find those formulae, we write a method in our `Topic` model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|class Topic < ActiveRecord::Base|
|[self, chapter, section, unit, *ancestors].compact.flat_map(&:formula_ids)/code>|
The method works. That said, it does so by making several queries to the database.
More queries = slower-running code. How do we reduce the number of queries, and how much faster will the code run?
Easy, tiger. We’re totally gonna get there. But first, let’s talk about whether we should—and how we know.
When is it important to optimize on code performance?
Test-Driven RecyclerViews in Android (the code is Java. This is inexplicably the second most viewed post on this blog, which tells me somebody out there is evangelizing it. Thanks, anonymous evangelist!)
Test-Driven Network Calls in iOS (mostly included because I had fun making a sample app about villains)
A note: I get that some of these posts might not be in languages you write. I’d encourage you to take a look anyway! It’s helpful to have a little bit of intuition about several different languages, and sometimes you’ll get the chance to cross-pollinate the wisdom and practices of one language or community to another one. And that’s a lot of fun! Also, regardless of language, I try to make the posts entertaining with example apps about fruits, villains, and candy.