0.0
The project is in a healthy, maintained state
Merge deeply nested hashes faster than DeepMerge or ActiveSupport
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

SinDeepMerge

License Tag Release Test Lint

Merge deeply nested hashes faster than DeepMerge or ActiveSupport.

  • Installation
  • Usage
    • Hash#deep_merge
    • Hash#deep_merge!
  • Benchmark
  • Changelog
  • Contributing
  • License
  • Code of Conduct
  • Sponsor

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add sin_deep_merge

If bundler is not being used to manage dependencies, install the gem by executing:

gem install sin_deep_merge

Usage

Hash#deep_merge

SinDeepMerge's Hash#deep_merge is compatible with ActiveSupport's Hash#deep_merge.

require 'sin_deep_merge'

hash1 = { a: 1, b: '2', c: :three, d: true, e: nil }
hash2 = { b: :two, c: nil, d: false, e: 0, f: 'f' }
hash1.deep_merge(hash2) # => { a: 1, b: :two, c: nil, d: false, e: 0, f: 'f' }

hash1 = { a: [1, 2], b: [3, 4] }
hash2 = { a: [3, 4], b: 5 }
hash1.deep_merge(hash2) # => { a: [3, 4], b: 5 }

hash1 = { a: { b: 1, c: 2 }, b: { c: 3 } }
hash2 = { a: { c: 3, d: 4 }, b: 5 }
hash1.deep_merge(hash2) # => { a: { b: 1, c: 3, d: 4 }, b: 5 }

hash1 = { a: 1, b: 2 }
hash2 = { b: 3, c: 4 }
hash1.deep_merge(hash2) { |_key, old_val, new_val| old_val + new_val } # => { a: 1, b: 5, c: 4 }

hash1 = { a: [1, 2], b: 3 }
hash2 = { a: [3, 4], b: 5 }
hash1.deep_merge(hash2) do |_key, old_val, new_val|
  if old_val.is_a?(Array) && new_val.is_a?(Array)
    old_val + new_val
  else
    new_val
  end
end # => { a: [1, 2, 3, 4], b: 5 }

Hash#deep_merge!

Hash#deep_merge! destructively updates self by merging new values directly into it.

Benchmark

SinDeepMerge's Hash#deep_merge is about 1.5-1.7x faster than ActiveSupport's Hash#deep_merge and about 3.6-5.3x faster than DeepMerge's Hash#deep_merge.

$ bundle exec rake benchmark

+-----------------------------------------------------------------+
|              Benchmark Result (Shallow Recursion)               |
+----------------------------+----------------------+-------------+
| Name                       | Iteration Per Second | Speed Ratio |
+----------------------------+----------------------+-------------+
| SinDeepMerge - deep_merge  | 1899218.6            | -           |
| Scratch - deep_merge       | 1289196.4            | 1.5x slower |
| ActiveSupport - deep_merge | 1215659.6            | 1.6x slower |
| DeepMerge - deep_merge     | 358813.8             | 5.3x slower |
+----------------------------+----------------------+-------------+

+-----------------------------------------------------------------+
|         Benchmark Result (Shallow Recursion With Block)         |
+----------------------------+----------------------+-------------+
| Name                       | Iteration Per Second | Speed Ratio |
+----------------------------+----------------------+-------------+
| SinDeepMerge - deep_merge  | 1594873.2            | -           |
| Scratch - deep_merge       | 1235536.5            | 1.3x slower |
| ActiveSupport - deep_merge | 1062621.4            | 1.5x slower |
| DeepMerge - deep_merge     | 356335.5             | 4.5x slower |
+----------------------------+----------------------+-------------+

+-----------------------------------------------------------------+
|                Benchmark Result (Deep Recursion)                |
+----------------------------+----------------------+-------------+
| Name                       | Iteration Per Second | Speed Ratio |
+----------------------------+----------------------+-------------+
| SinDeepMerge - deep_merge  | 23017.8              | -           |
| ActiveSupport - deep_merge | 14626.7              | 1.6x slower |
| Scratch - deep_merge       | 14271.6              | 1.6x slower |
| DeepMerge - deep_merge     | 6277.8               | 3.7x slower |
+----------------------------+----------------------+-------------+

+-----------------------------------------------------------------+
|          Benchmark Result (Deep Recursion With Block)           |
+----------------------------+----------------------+-------------+
| Name                       | Iteration Per Second | Speed Ratio |
+----------------------------+----------------------+-------------+
| SinDeepMerge - deep_merge  | 22727.1              | -           |
| Scratch - deep_merge       | 14154.0              | 1.6x slower |
| ActiveSupport - deep_merge | 13661.2              | 1.7x slower |
| DeepMerge - deep_merge     | 6279.9               | 3.6x slower |
+----------------------------+----------------------+-------------+

The benchmark was executed in the following environment:

ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +YJIT +PRISM [arm64-darwin24]

Changelog

See CHANGELOG.md.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cadenza-tech/sin_deep_merge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the SinDeepMerge project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Sponsor

You can sponsor this project on Patreon.