Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_model/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(object, options={})
@each_serializer = options[:each_serializer]
@options = options.merge(root: nil)
end
attr_accessor :object, :root, :meta_key, :meta
attr_accessor :object, :root, :meta_key, :meta, :options

def json_key
if root.nil?
Expand Down
3 changes: 2 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ def initialize(object, options={})
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@options = options.reject{|k,v| [:scope, :root, :meta_key, :meta].include?(k) }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this in incur an Array allocation on every loop through the @options list or would extracting that out to a constant be too much of a micro-optimization?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nixme it definitely does. I wrote up a quick benchmark script to test the two methods very simply at https://gist.github.com/jasontruluck/7722888 . From the results I would say for an average use case it is negligible. At n=10 results list there is almost no discernible difference and even at 1,000,000 items is only fractions of a second. That being said, moving this out into a constant is a super easy change that can be made very easily. So even though I would consider it a micro-optimization there is no real harm in it to me, but I would like to differ to @spastorino as to whether this change is worth a PR or not.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do the PR if @spastorino think's it's worth it. Thanks for running the numbers!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem at all!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 let's move this into a constant

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming it properly would also express better the intention

end
attr_accessor :object, :scope, :meta_key, :meta, :root
attr_accessor :object, :scope, :meta_key, :meta, :root, :options

def json_key
if root == true || root.nil?
Expand Down
17 changes: 17 additions & 0 deletions test/unit/active_model/serializer/options_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'test_helper'

module ActiveModel
class Serializer
class OptionsTest < ActiveModel::TestCase
def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
end

def test_meta
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', random_option: "This is an option")

assert_equal("This is an option", profile_serializer.options[:random_option])
end
end
end
end