Skip to content

Merge deeply nested hashes faster than DeepMerge or ActiveSupport.

License

Notifications You must be signed in to change notification settings

cadenza-tech/sin_deep_merge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

101 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SinDeepMerge

License Tag Release Test Lint

Merge deeply nested hashes faster than DeepMerge or ActiveSupport.

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 6.8-12.0x faster than DeepMerge's Hash#deep_merge and about 2.8-4.8x faster than ActiveSupport's Hash#deep_merge.

$ bundle exec rake benchmark

+------------------------------------------------------------------+
|               Benchmark Result (Shallow Recursion)               |
+----------------------------+----------------------+--------------+
| Name                       | Iteration Per Second | Speed Ratio  |
+----------------------------+----------------------+--------------+
| SinDeepMerge - deep_merge  | 4906360.5            | Fastest      |
| Scratch - deep_merge       | 1394443.3            | 3.5x slower  |
| ActiveSupport - deep_merge | 1114420.2            | 4.4x slower  |
| DeepMerge - deep_merge     | 410338.3             | 12.0x slower |
+----------------------------+----------------------+--------------+

+------------------------------------------------------------------------------------------------+
|                        Benchmark Result (Shallow Recursion With Block)                         |
+-----------------------------------------------------------+----------------------+-------------+
| Name                                                      | Iteration Per Second | Speed Ratio |
+-----------------------------------------------------------+----------------------+-------------+
| SinDeepMerge - deep_merge (Shallow Recursion With Block)  | 2734823.1            | Fastest     |
| Scratch - deep_merge (Shallow Recursion With Block)       | 1378402.4            | 2.0x slower |
| ActiveSupport - deep_merge (Shallow Recursion With Block) | 972886.7             | 2.8x slower |
| DeepMerge - deep_merge (Shallow Recursion With Block)     | 401372.9             | 6.8x slower |
+-----------------------------------------------------------+----------------------+-------------+

+-----------------------------------------------------------------+
|                Benchmark Result (Deep Recursion)                |
+----------------------------+----------------------+-------------+
| Name                       | Iteration Per Second | Speed Ratio |
+----------------------------+----------------------+-------------+
| SinDeepMerge - deep_merge  | 63005.6              | Fastest     |
| Scratch - deep_merge       | 16103.6              | 3.9x slower |
| ActiveSupport - deep_merge | 13200.2              | 4.8x slower |
| DeepMerge - deep_merge     | 7003.9               | 9.0x slower |
+----------------------------+----------------------+-------------+

+---------------------------------------------------------------------------------------------+
|                        Benchmark Result (Deep Recursion With Block)                         |
+--------------------------------------------------------+----------------------+-------------+
| Name                                                   | Iteration Per Second | Speed Ratio |
+--------------------------------------------------------+----------------------+-------------+
| SinDeepMerge - deep_merge (Deep Recursion With Block)  | 58192.8              | Fastest     |
| Scratch - deep_merge (Deep Recursion With Block)       | 15931.3              | 3.7x slower |
| ActiveSupport - deep_merge (Deep Recursion With Block) | 12269.7              | 4.7x slower |
| DeepMerge - deep_merge (Deep Recursion With Block)     | 6997.5               | 8.3x slower |
+--------------------------------------------------------+----------------------+-------------+

The benchmark was executed in the following environment:

  • Ruby 3.4.5 (2025-07-16 revision 20cda200d3) +YJIT +PRISM [arm64-darwin24]
  • DeepMerge 1.2.2
  • ActiveSupport 8.0.2

Changelog

See CHANGELOG.md.

Development

Building for JRuby

To build the Java extension and run tests for JRuby support:

./script/jruby_build_and_test.sh

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.

About

Merge deeply nested hashes faster than DeepMerge or ActiveSupport.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project