Common Bottlenecks
Speed up Ruby code.
Recognizing Bottlenecks
Most Ruby slowdowns come from a few recurring patterns. Knowing them lets you fix code quickly.
- Wrong data structure for the job
- Repeated work that could be cached
- N+1 queries and nested loops
This lesson covers the most common offenders.
puts 'Speeding up Ruby starts with knowing the usual suspects'Array#include? vs Set
Checking membership with Array#include? is O(n). A Set (or hash) gives O(1) lookups.
- For repeated membership checks, build a Set once
- Huge speedup on large collections
require 'set'
allowed = Set.new(['a', 'b', 'c'])
puts allowed.include?('b')
puts allowed.include?('z')All lessons in this course
- Measuring Performance
- Profiling Tools
- Memory Optimization
- Common Bottlenecks