fastruby /fast-ruby
:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
★ 5,741⑂ 368👁 159⑃ 182● 71▤ 0
-1
日增长
5,741
stars
368
forks
159
watchers
182
prs
71
issues
0
releases
README
Fast Ruby
=======================================================================================================================================================================
In Erik Michaels-Ober's great talk, 'Writing Fast Ruby': Video @ Baruco 2014, Slide, he presented us with many idioms that lead to faster running Ruby code. He inspired me to document these to let more people know. I try to link to real commits so people can see that this can really have benefits in the real world. This does not mean you can always blindly replace one with another. It depends on the context (e.g. gsub versus tr). Friendly reminder: Use with caution!
Each idiom has a corresponding code example that resides in code.
All results listed in README.md are running with Ruby 4.0.0 on macOS 15.6.1. Machine information: MacBook Pro (14-inch, Nov 2024), Apple M4 Pro, 48 GB RAM. Your results may vary, but you get the idea. : )
You can checkout the GitHub Actions build for these benchmark results ran against different Ruby implementations.
Let's write faster code, together! https://www.rubydoc.info/stdlib/core/Module:attr_accessor
$ ruby -v code/general/attr-accessor-vs-getter-and-setter.rb
ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [arm64-darwin24]
Warming up --------------------------------------
getter_and_setter 1.122M i/100ms
attr_accessor 1.303M i/100ms
Calculating -------------------------------------
getter_and_setter 10.999M (± 2.1%) i/s (90.92 ns/i) - 56.083M in 5.101077s
attr_accessor 12.517M (± 1.5%) i/s (79.89 ns/i) - 63.841M in 5.101477s
Comparison:
attr_accessor: 12517008.0 i/s
getter_and_setter: 10999275.5 i/s - 1.14x slower
begin...rescue vs respond_to? for Control Flow code
$ ruby -v code/general/begin-rescue-vs-respond-to.rb
ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [arm64-darwin24]
Warming up --------------------------------------
begin...rescue 230.601k i/100ms
respond_to? 1.915M i/100ms
Calculating -------------------------------------
begin...rescue 2.390M (± 0.9%) i/s (418.40 ns/i) - 11.991M in 5.017466s
respond_to? 18.745M (± 1.8%) i/s (53.35 ns/i) - 93.843M in 5.007990s
Comparison:
respond_to?: 18744998.8 i/s
begin...rescue: 2390076.1 i/s - 7.84x slower
define_method vs module_eval for Defining Methods code
$ ruby -v code/general/define_method-vs-module-eval.rb
ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [arm64-darwin24]
Warming up --------------------------------------
module_eval with string
630.000 i/100ms
define_method 685.000 i/100ms
Calculating -------------------------------------
module_eval with string
5.633k (±18.1%) i/s (177.53 μs/i) - 27.090k in 5.009884s
define_method 7.239k (±17.6%) i/s (138.14 μs/i) - 34.250k in 5.040522s
Comparison:
define_method: 723Top issues
#96Some benchmarks are measured with wrong approach17 comments · 2016-01-25#208Parallel versus sequential assignment is microbenchmark (not representative) and scales with number of inputs7 comments · 2022-08-31#220`str.count("\n")` is 1.3-170 times faster than `str.lines.count` or `str.each_line.count` depending on the string size6 comments · 2023-09-11#98reverse.detect vs select { ... }.last5 comments · 2016-02-11#206[FEATURE REQUEST] Use the project's WIKI to share results for each Ruby version with latest CI results4 comments · 2022-07-11#158String concat contains dummy comparision. 4 comments · 2018-06-12#185`String#match` vs `String#scan`3 comments · 2020-08-20#104Benchmark with memory allocations3 comments · 2016-03-25