{"id":19724176,"url":"https://github.com/fastruby/benches","last_synced_at":"2025-10-07T12:12:13.121Z","repository":{"id":40006627,"uuid":"88983030","full_name":"fastruby/benches","owner":"fastruby","description":"A few benchmarks we run for the Ombu Labs blog","archived":false,"fork":false,"pushed_at":"2023-07-13T23:38:16.000Z","size":68,"stargazers_count":5,"open_issues_count":3,"forks_count":0,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-05T20:04:25.098Z","etag":null,"topics":["benchmarking","ruby","ruby-benchmarks"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastruby.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":"code-of-conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-21T12:46:52.000Z","updated_at":"2022-11-04T22:43:41.000Z","dependencies_parsed_at":"2024-11-11T23:28:12.910Z","dependency_job_id":"40461a1b-2cef-4d8f-8eea-a65964e5434c","html_url":"https://github.com/fastruby/benches","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastruby%2Fbenches","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastruby%2Fbenches/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastruby%2Fbenches/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastruby%2Fbenches/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastruby","download_url":"https://codeload.github.com/fastruby/benches/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251593025,"owners_count":21614460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["benchmarking","ruby","ruby-benchmarks"],"created_at":"2024-11-11T23:24:46.916Z","updated_at":"2025-10-07T12:12:08.095Z","avatar_url":"https://github.com/fastruby.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Benches\n\nA collection of Ruby benchmarks for fun and for the [Ombu Labs blog](https://www.ombulabs.com/blog)\n\n## Setup\n\nYou can get the project locally setup with this command:\n\n    ./bin/setup\n\n## Run\n\nYou can run all benchmarks with this command:\n\n    bundle exec rake\n\n## Struct vs Hash\n\nWhat's most efficient? A Struct or a Hash? Let's create one of each, access them,\nand compare the performance using `benchmark-ips`:\n\n    require 'benchmark/ips'\n\n    Phone = Struct.new(:number)\n\n    Benchmark.ips do |x|\n\n      x.report(\"struct\") do\n\n        20.times do |i|\n          str = Phone.new(i)\n          str.number\n        end\n      end\n\n      x.report(\"hash\") do\n        20.times do |i|\n          str = Hash.new(number: i)\n          str[:number]\n        end\n      end\n    end\n\n\nHere is the result:\n\n    $ bundle exec ruby benchmarks/struct_vs_hash.rb\n    Ruby version: 2.3.3\n    Warming up --------------------------------------\n              struct    18.585k i/100ms\n                hash     3.685k i/100ms\n    Calculating -------------------------------------\n              struct    211.043k (± 8.0%) i/s -      1.059M in   5.056961s\n                hash     43.300k (± 3.4%) i/s -    217.415k in   5.026906s\n\n    Comparison:\n              struct:   211043.3 i/s\n                hash:    43299.6 i/s - 4.87x  slower\n\n## Select + First vs. Detect\n\nWhat's fastest `array.select {|x| condition(x) }.first` or\n`array.detect {|x| condition(x) }`?\n\nHere is the result:\n\n    $ bundle exec ruby benchmarks/select_vs_detect.rb\n    Ruby version: 2.3.3\n    Warming up --------------------------------------\n            select-first     5.037k i/100ms\n                  detect     8.828k i/100ms\n    Calculating -------------------------------------\n            select-first     51.482k (± 2.3%) i/s -    261.924k in   5.090419s\n                  detect     91.767k (± 3.4%) i/s -    459.056k in   5.008376s\n\n    Comparison:\n                  detect:    91767.2 i/s\n            select-first:    51481.9 i/s - 1.78x  slower\n\n## Map + Flatten vs. flat_map\n\nWhat's fastest `array.map {|x| process(x) }.flatten` or\n`array.flat_map {|x| process(x) }`?\n\nHere is the result:\n\n    $ bundle exec ruby benchmarks/map_flatten_vs_flat_map.rb\n    Ruby version: 2.3.3\n    Warming up --------------------------------------\n             map-flatten     1.779k i/100ms\n                flat_map     2.255k i/100ms\n    Calculating -------------------------------------\n             map-flatten     17.565k (± 7.8%) i/s -     87.171k in   5.000807s\n                flat_map     22.575k (± 6.1%) i/s -    112.750k in   5.016034s\n\n    Comparison:\n                flat_map:    22575.4 i/s\n             map-flatten:    17565.4 i/s - 1.29x  slower\n\n## Time.parse vs. Time.at\n\nWhat's fastest `Time.parse` or `Time.at`?\n\nHere is the result:\n\n    $ bundle exec ruby benchmarks/time/parse_vs_at.rb\n    Ruby version: 2.3.3\n    Warming up --------------------------------------\n              Time.parse     2.761k i/100ms\n                 Time.at    56.088k i/100ms\n    Calculating -------------------------------------\n              Time.parse     27.667k (± 7.4%) i/s -    138.050k in   5.019750s\n                 Time.at    700.829k (± 4.4%) i/s -      3.534M in   5.052682s\n\n    Comparison:\n                 Time.at:   700829.3 i/s\n              Time.parse:    27667.2 i/s - 25.33x  slower\n\n## unless object.nil? vs. if object\n\nWhat's fastest `unless object.nil?` or `if object`?\n\nHere is the result:\n\n    $ bundle exec ruby benchmarks/unless_nil_vs_if_object.rb\n    Ruby version: 2.3.3\n    Warming up --------------------------------------\n          unless nil   251.994k i/100ms\n           if object   267.552k i/100ms\n    Calculating -------------------------------------\n          unless nil      9.435M (± 6.9%) i/s -     47.123M in   5.021124s\n           if object     11.431M (± 4.9%) i/s -     57.256M in   5.021363s\n\n    Comparison:\n           if object: 11431148.2 i/s\n          unless nil:  9435226.8 i/s - 1.21x  slower\n\n## Rails Benchmarks\n\nWhat about Rails-specific code? Let's do some benchmarks comparing different\nways to achieve the same result.\n\n## map vs. pluck\n\nWhat's fastest `map` or `pluck`?\n\nHere is the result:\n\n    $ bundle exec rake benches:pluck_vs_map\n    Warming up --------------------------------------\n               map(\u0026:id)    18.000  i/100ms\n              pluck(:id)   107.000  i/100ms\n    Calculating -------------------------------------\n               map(\u0026:id)    193.198  (±12.9%) i/s -    954.000  in   5.024860s\n              pluck(:id)      1.056k (± 5.0%) i/s -      5.350k in   5.082911s\n\n    Comparison:\n              pluck(:id):     1055.8 i/s\n               map(\u0026:id):      193.2 i/s - 5.47x  slower\n\n\n## Guidelines\n\nFeel free to add more benchmark files in this project. The blocks which are\nbeing compared should be simple and straightforward.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastruby%2Fbenches","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastruby%2Fbenches","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastruby%2Fbenches/lists"}