fastruby /fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.

★ 5,741⑂ 368👁 159⑃ 182● 71▤ 0
HTMLCreated 2014-10-13github.com/fastruby/fast-ruby
-1
Daily
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:     723

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON