Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use each_with_object instead of Hash[map] #6

Merged

Conversation

krzysiek1507
Copy link
Contributor

No description provided.

@kpheasey
Copy link
Contributor

kpheasey commented Oct 4, 2019

@krzysiek1507 Is this a performance optimization?

1 similar comment
@kpheasey
Copy link
Contributor

kpheasey commented Oct 4, 2019

@krzysiek1507 Is this a performance optimization?

@krzysiek1507
Copy link
Contributor Author

Yes.

# frozen_string_literal: true

require 'benchmark/ips'
require 'benchmark/memory'

class DeepSymbolize
  class << self
    def deep_symbolize(collection)
      if collection.is_a? Hash
        Hash[collection.map do |k, v|
          [k.to_sym, deep_symbolize(v)]
        end]
      elsif collection.is_a? Array
        collection.map { |i| deep_symbolize(i) }
      else
        collection.to_sym
      end
    end
  end
end

class DeepSymbolizeOptimized
  class << self
    def deep_symbolize(collection)
      if collection.is_a? Hash
        collection.each_with_object({}) do |(k, v), hsh|
          hsh[k.to_sym] = deep_symbolize(v)
        end
      elsif collection.is_a? Array
        collection.map { |i| deep_symbolize(i) }
      else
        collection.to_sym
      end
    end
  end
end

hsh = { 'a' => 'v', 'b' => { 'b2' => {} } }
hsh = { :a => 'v', :b => { :b2 => {} }, c: [{ c1: '' }] }

[:ips, :memory].each do |type|
  puts "#{type} benchmark:"
  puts

  Benchmark.public_send(type) do |x|
    x.report('original') { DeepSymbolize.deep_symbolize hsh }
    x.report('patch') { DeepSymbolizeOptimized.deep_symbolize hsh }

    x.compare!
  end
end
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
ips benchmark:

Warming up --------------------------------------
            original    21.332k i/100ms
               patch    21.833k i/100ms
Calculating -------------------------------------
            original    217.069k (± 7.3%) i/s -      1.088M in   5.040936s
               patch    264.518k (± 9.4%) i/s -      1.332M in   5.085691s

Comparison:
               patch:   264518.5 i/s
            original:   217068.7 i/s - 1.22x  slower

memory benchmark:

Calculating -------------------------------------
            original     1.328k memsize (     0.000  retained)
                        14.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
               patch     1.168k memsize (     0.000  retained)
                        10.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)

Comparison:
               patch:       1168 allocated
            original:       1328 allocated - 1.14x more

@kpheasey kpheasey merged commit f2a1934 into jsonapi-serializer:dev Oct 4, 2019
@krzysiek1507 krzysiek1507 deleted the refactor/use-each-with-object branch October 5, 2019 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants