Ruby hash vs. RubyTree

I had to figure out what will perform better. A simple hash or a tree using RubyTree

I choose to implement JEGII’s example. Once using the simple ‘Hash’ and once using 'RubyTree’.

The example

Input is a list of “Outline-Tags” which represents the structure of tree.


Output should be a HTML UL

  • 001
    • 001.001
    • 001.002 …..

Solution one – simple Hash

File render_list_hash will create a Hash like:

{ "001" => { "001" => { "001" => {}, "002" => {} ... }}

and then renders this hash into a HTML-UL-tree.

Solution two - using RubyTree

File render_list_tree will create a 'Tree::TreeNode’ with children, like so:

                 001         002
              001   002

and then renders the root-node recursively into a HTML-UL-tree.


You can run benchmark.rb to compare the two solutions. And it turns out that a simple Hash performs a lot better.

The reason maybe is, that the hash-solution creates one single hash and fills it with entries, where the Tree-solution creates a ton of Ruby-objects.

Anyways, the Tree-Object can do a lot more than a simple Hash. So it’s not fair to compare this way. My intention was to figure out how big the expected performance-costs will be. And it turns out, it’s to much.