wmakley / tiny_serializer

Simple Ruby JSON Serialization DSL. Replaces active_model_serializers.
MIT License
53 stars 2 forks source link

Instantiate new serializer class for each collection item #8

Closed trevorrjohn closed 6 years ago

trevorrjohn commented 6 years ago

If you pass in an array of objects, each object should get it's own serializer instance.

Benchmark shows that the performance is not a blocker:

https://github.com/trevorrjohn/simple_serializer/blob/tj/benchmark/benchmark.rb

Rehearsal ------------------------------------------------------------
existing serializer:       0.041254   0.001016   0.042270 (  0.042298)
new instance serializer:   0.036131   0.001481   0.037612 (  0.043380)
--------------------------------------------------- total: 0.079882sec

                               user     system      total        real
existing serializer:       0.031271   0.000368   0.031639 (  0.031770)
new instance serializer:   0.038386   0.002491   0.040877 (  0.047027)
wmakley commented 6 years ago

Thanks, my own version of this has been applied to master already! I like some of your style changes, but return [] if @object.empty? introduces a regression when applied to ActiveRecord::Relations. The only assumption I make about collections is that they implement each.

trevorrjohn commented 6 years ago

Sounds good. Makes sense with ActiveRecord::Relations, I didn't think about that.

trevorrjohn commented 6 years ago

Although, I am not seeing the new code on master yet.

wmakley commented 6 years ago

Should be up! I tagged a new version and stuff, I just called it 0.5.0 since it's a behavior change.