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

Take items of original array instead of dup and delete #9

Merged

Conversation

krzysiek1507
Copy link
Contributor

No description provided.

@kpheasey
Copy link
Contributor

kpheasey commented Oct 4, 2019

@krzysiek1507 This looks like it could be a a big performance boost, did you benchmark it?

@krzysiek1507
Copy link
Contributor Author

# frozen_string_literal: true

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

def remaining_items(items)
  return unless items.size > 1

  items_copy = items.dup
  items_copy.delete_at(0)
  [items_copy.join('.').to_sym]
end

def remaining_items_opt(items)
  return unless items.size > 1

  [items[1..-1].join('.').to_sym]
end

items = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

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

  Benchmark.public_send(type) do |x|
    x.report('original') { remaining_items items }
    x.report('patch') { remaining_items_opt items }

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

Warming up --------------------------------------
            original    56.524k i/100ms
               patch    63.157k i/100ms
Calculating -------------------------------------
            original    704.188k (± 3.3%) i/s -      3.561M in   5.063434s
               patch    809.425k (± 7.4%) i/s -      4.042M in   5.030366s

Comparison:
               patch:   809425.0 i/s
            original:   704188.2 i/s - 1.15x  slower

memory benchmark:

Calculating -------------------------------------
            original   464.000  memsize (     0.000  retained)
                         7.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
               patch   408.000  memsize (     0.000  retained)
                         7.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)

Comparison:
               patch:        408 allocated
            original:        464 allocated - 1.14x more

@kpheasey kpheasey merged commit 44a896d into jsonapi-serializer:dev Oct 4, 2019
@krzysiek1507 krzysiek1507 deleted the refactor/remaining-items branch October 5, 2019 11:07
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