light_serializer 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc42bdb60c1c303a13fa6cb066dceb052f762612019c932c7db38363f8b5df94
4
- data.tar.gz: 2464fcf561ba306e7c8c6b6377d9c3d4ed6f3ad9f0249b9836de002505bbcc56
3
+ metadata.gz: 25bb1a6af47fb69f5eabb3aaba9b52c2323751b86c3abd229c20935320f06c2c
4
+ data.tar.gz: 57b67a1e1d29806e0028c6f9b7ad069b99ba57253e99adcef249dd292b1cf633
5
5
  SHA512:
6
- metadata.gz: '00884b745cedcac93b091fbdc3e58ce2c9d848112134580fa42c057736e680e26318fb4e3a09932fbd0781d504f51a871fc50a1a318e9ec8d6e7d56d4b6a4d4f'
7
- data.tar.gz: 4a4b08cdc134007968182bcf6abf4512ccd720e8dc343f5367c75b0f7dcacb3b82d078f300a5f62086b1cba84d4c65cdfe2957764ef1fa5f1b0629ec5af10af4
6
+ metadata.gz: 6c6f7a76a1e8c6f618547a9d0dbbc6e0a475ae2d009b0122b4b32de9572b378c6ebd61ee1dc41ddb74e0eed27be1afa1644f584cbd5a46c7959f95dbbd8a9925
7
+ data.tar.gz: f3e6c53d11d2495711506281626c037f7870d061115cbafcb0f9b6741969dd9f4ef0615c8f310a0751ff65cd4c42342f0f1ad9e9cb3b5d6d92a1119dea1d34a3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- light_serializer (0.0.5)
4
+ light_serializer (0.0.6)
5
5
  oj (~> 3)
6
6
 
7
7
  GEM
@@ -37,7 +37,7 @@ GEM
37
37
  kwalify (0.7.2)
38
38
  method_source (0.9.0)
39
39
  minitest (5.11.3)
40
- oj (3.7.0)
40
+ oj (3.7.1)
41
41
  parallel (1.12.1)
42
42
  parser (2.5.1.2)
43
43
  ast (~> 2.4.0)
data/README.md CHANGED
@@ -1,4 +1,186 @@
1
- # light_serializer
1
+ # Light Serializer
2
+
2
3
  Light and Fast serializer
3
4
 
4
5
  [![Build Status](https://travis-ci.org/krim/light_serializer.svg?branch=master)](https://travis-ci.org/krim/light_serializer)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/b9167078ec6b75117d0d/maintainability)](https://codeclimate.com/github/krim/light_serializer/maintainability)
7
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/b9167078ec6b75117d0d/test_coverage)](https://codeclimate.com/github/krim/light_serializer/test_coverage)
8
+
9
+ LS is a JSON Object Presenter that takes business objects and breaks them down into simple hashes and serializes them to JSON. It can be used in Rails(or other ruby application) in place of other serializers (like JBuilder or ActiveModelSerializers). It is designed to be simple, direct, and performant.
10
+
11
+ ### Performant comparison
12
+ All performant comparison results are available [here](https://github.com/krim/light_serializer/blob/master/performance_comparison.md). You can run benchmark by yourself.
13
+ ```bash
14
+ git clone git@github.com:krim/light_serializer.git
15
+ cd light_serializer
16
+ ruby bench.rb
17
+
18
+ ```
19
+
20
+ ## Table of Contents
21
+
22
+ * [Install](#install)
23
+ * [Usage](#usage)
24
+ * [Basic](#basic)
25
+ * [Custom method names](#custom-method-names)
26
+ * [Associations](#associations)
27
+ * [Custom root](#custom-root)
28
+ * [Collection serializer](#collection-serializer)
29
+
30
+ ## Install
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'light_serializer'
36
+ ```
37
+
38
+ Execute:
39
+
40
+ ```bash
41
+ $ bundle install
42
+ ```
43
+
44
+
45
+ ## Usage
46
+ ### Basic
47
+ If you have an object you would like serialized, simply create a serializer. Say, for example, you have a User record with the following attributes `[:uuid, :email, :first_name, :last_name, :address]`.
48
+
49
+ You may define a simple serializer like so:
50
+
51
+ ```ruby
52
+ class UserSeriaizer < LightSerializer::Serializer
53
+ attributes :uuid, :first_name, :last_name, :email
54
+ end
55
+ ```
56
+
57
+ and then, in your code:
58
+ ```ruby
59
+ puts UserSeriaizer.new(user).to_json # Output is a JSON string
60
+ ```
61
+
62
+ And the output would look like:
63
+
64
+ ```json
65
+ {
66
+ "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
67
+ "first_name": "John",
68
+ "last_name": "Doe",
69
+ "email": "john.doe@some.fake.email.domain"
70
+ }
71
+ ```
72
+
73
+ ### Custom method names
74
+ You can rename the resulting JSON keys in both fields and associations by defining custom method:
75
+
76
+ ```ruby
77
+ class UserSeriaizer < LightSerializer::Serializer
78
+ attributes :id, :first_name, :last_name, :email
79
+
80
+ def id
81
+ object.uuid
82
+ end
83
+ end
84
+ ```
85
+
86
+ This will result in JSON that looks something like this:
87
+
88
+ ```json
89
+ {
90
+ "id": "92a5c732-2874-41e4-98fc-4123cd6cfa86",
91
+ "first_name": "John",
92
+ "last_name": "Doe",
93
+ "email": "john.doe@some.fake.email.domain"
94
+ }
95
+ ```
96
+
97
+ ### Associations
98
+ You may include associated objects. Say, for example, a user has projects:
99
+
100
+ ```ruby
101
+ class ProjectSerializer < LightSerializer::Serializer
102
+ attributes :uuid, :name
103
+ end
104
+
105
+ class UserSeriaizer < LightSerializer::Serializer
106
+ attributes(
107
+ :uuid,
108
+ :first_name,
109
+ :last_name,
110
+ :email,
111
+ { projects: ProjectSerializer }
112
+ )
113
+ end
114
+ ```
115
+
116
+ Usage:
117
+ ```ruby
118
+ puts UserSeriaizer.new(user).to_json
119
+ ```
120
+
121
+ Output:
122
+ ```json
123
+ {
124
+ "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
125
+ "first_name": "John",
126
+ "last_name": "Doe",
127
+ "email": "john.doe@some.fake.email.domain",
128
+ "projects": [
129
+ {
130
+ "uuid": "dca94051-4195-42bc-a9aa-eb99f7723c82",
131
+ "name": "Beach Cleanup"
132
+ },
133
+ {
134
+ "uuid": "eb881bb5-9a51-4d27-8a29-b264c30e6160",
135
+ "name": "Storefront Revamp"
136
+ }
137
+ ]
138
+ }
139
+ ```
140
+
141
+ ### Custom root
142
+ By default, returned JSON doesn't have root. You may specify it by the option in serializer's initializer:
143
+ ```ruby
144
+ puts UserSeriaizer.new(user, root: :person).to_json
145
+ ```
146
+
147
+ Output:
148
+ ```json
149
+ {
150
+ "person": {
151
+ "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
152
+ "first_name": "John",
153
+ "last_name": "Doe",
154
+ "email": "john.doe@some.fake.email.domain"
155
+ }
156
+ }
157
+ ```
158
+
159
+ ### Collection serializer
160
+ Use `LightSerializer::SerializeCollection` to serialize collection of objects. You have to specify seriaizer, and can specify root and context.
161
+ ```ruby
162
+ puts LightSerializer::SerializeCollection.new(users, serializer: UserSerializer, root: :users).to_json
163
+ ```
164
+
165
+ Output:
166
+ ```json
167
+ {
168
+ "users": [
169
+ {
170
+ "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
171
+ "first_name": "John",
172
+ "last_name": "Doe",
173
+ "email": "john.doe@some.fake.email.domain"
174
+ },
175
+ {
176
+ "uuid": "262c3ec743af-4719-4719-8f21-733f0758",
177
+ "first_name": "Foo",
178
+ "last_name": "Bar",
179
+ "email": "foo.bar@some.fake.email.domain"
180
+ },
181
+ {
182
+ ...
183
+ }
184
+ ]
185
+ }
186
+ ```
data/bench.rb CHANGED
@@ -1,12 +1,20 @@
1
1
  # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
2
- require 'surrealist'
3
- require_relative 'lib/light_serializer'
4
- require 'benchmark/ips'
5
- require 'active_record'
6
- require 'active_model'
7
- require 'active_model_serializers'
8
- require 'blueprinter'
9
- require 'pry'
2
+ require 'bundler/inline'
3
+
4
+ gemfile do
5
+ source 'https://rubygems.org'
6
+
7
+ gem 'activerecord', require: 'active_record'
8
+ gem 'activemodel'
9
+ gem 'sqlite3'
10
+ gem 'light_serializer'
11
+ gem 'active_model_serializers'
12
+ gem 'blueprinter'
13
+ gem 'surrealist'
14
+ gem 'benchmark-ips', require: 'benchmark/ips'
15
+ end
16
+
17
+ puts 'Gems installed and loaded!'
10
18
 
11
19
  ActiveRecord::Base.establish_connection(
12
20
  adapter: 'sqlite3',
@@ -204,7 +212,7 @@ def benchmark_collection(ams_arg: '', oj_arg: '')
204
212
  ).to_json
205
213
  end,
206
214
  -> { Surrealist.surrealize_collection(users) },
207
- -> { UserLightSerializer.new(users).to_json },
215
+ -> { LightSerializer::SerializeCollection.new(users, serializer: UserLightSerializer).to_json },
208
216
  -> { UserSurrealistSerializer.new(users).surrealize },
209
217
  -> { users.to_json(only: %i[name email]) },
210
218
  -> { UserBlueprint.render(users) }]
@@ -251,7 +259,7 @@ def benchmark_associations_collection
251
259
  ).to_json
252
260
  end,
253
261
  -> { Surrealist.surrealize_collection(collection) },
254
- -> { AuthorLightSerializer.new(collection).to_json },
262
+ -> { LightSerializer::SerializeCollection.new(collection, serializer: AuthorLightSerializer).to_json },
255
263
  -> { AuthorSurrealistSerializer.new(collection).surrealize },
256
264
  lambda do
257
265
  collection.to_json(only: %i[name last_name age], methods: %i[full_name],
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'light_serializer/helpers/with_custom_root'
4
-
5
3
  module LightSerializer
6
4
  class HashedObject
7
5
  include Helpers::WithCustomRoot
@@ -38,17 +36,25 @@ module LightSerializer
38
36
  end
39
37
 
40
38
  def values_from_nested_resource(attribute, object, result)
41
- attribute_name = attribute.keys.last
42
- nested_serializer = attribute.values.last
39
+ attribute_name = attribute.keys[-1]
40
+ nested_serializer = attribute.values[-1]
43
41
 
44
42
  value = obtain_value(object, attribute_name)
45
43
  result[attribute_name] = hashed_nested_resource(value, nested_serializer)
46
44
  end
47
45
 
48
46
  def hashed_nested_resource(value, nested_serializer)
49
- return hashed_entity(value, nested_serializer) unless value.is_a?(Enumerable)
47
+ return hashed_entity(value, nested_serializer) unless can_be_enumerated?(value)
48
+
49
+ value.each_with_object(nested_serializer).map { |entity, serializer| hashed_entity(entity, serializer) }
50
+ end
51
+
52
+ def can_be_enumerated?(value)
53
+ value.is_a?(Enumerable) || active_record_relation?(value)
54
+ end
50
55
 
51
- value.each_with_object(nested_serializer).map(&method(:hashed_entity))
56
+ def active_record_relation?(object)
57
+ !!(defined?(ActiveRecord::Relation) && object.is_a?(ActiveRecord::Relation))
52
58
  end
53
59
 
54
60
  def hashed_entity(entity, nested_serializer)
@@ -76,7 +82,7 @@ module LightSerializer
76
82
  end
77
83
 
78
84
  def values_from_for_array(attribute, value, result)
79
- value.map { |entity| obtain_values(attribute, entity, result) }
85
+ value.map { |entity| entity.is_a?(Hash) ? entity : obtain_values(attribute, entity, result) }
80
86
  end
81
87
  end
82
88
  end
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module LightSerializer::Helpers
4
- module WithCustomRoot
5
- def with_custom_root(root)
6
- root ? { root.to_sym => yield } : yield
3
+ module LightSerializer
4
+ module Helpers
5
+ module WithCustomRoot
6
+ def with_custom_root(root)
7
+ root ? { root.to_sym => yield } : yield
8
+ end
7
9
  end
8
10
  end
9
11
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'light_serializer/helpers/with_custom_root'
4
- require 'light_serializer/serializer'
5
-
6
3
  module LightSerializer
7
4
  class SerializeCollection
8
5
  include Helpers::WithCustomRoot
@@ -1,16 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'oj'
4
- require 'light_serializer/hashed_object'
5
-
6
3
  module LightSerializer
7
4
  class Serializer
8
- attr_reader :object, :root, :context
5
+ attr_reader :object, :root, :context, :meta
9
6
 
10
7
  def self.attributes(*new_attributes)
11
8
  return @attributes if new_attributes.empty?
12
9
 
13
- @attributes = @attributes ? @attributes + new_attributes : new_attributes
10
+ @attributes = @attributes ? @attributes.concat(new_attributes) : new_attributes
14
11
  end
15
12
 
16
13
  def self.inherited(subclass)
@@ -18,18 +15,19 @@ module LightSerializer
18
15
  super(subclass)
19
16
  end
20
17
 
21
- def initialize(object, root: nil, context: nil)
18
+ def initialize(object, root: nil, context: nil, meta: nil)
22
19
  @object = object
23
20
  @root = root
24
21
  @context = context
22
+ @meta = meta
25
23
  end
26
24
 
27
25
  def to_json
28
- Oj.dump(hashed_object, mode: :compat)
26
+ Oj.dump(to_hash, mode: :compat)
29
27
  end
30
28
 
31
29
  def to_hash
32
- hashed_object
30
+ meta ? hashed_object.merge!(meta: meta) : hashed_object
33
31
  end
34
32
 
35
33
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LightSerializer
4
- VERSION = '0.0.5'
4
+ VERSION = '0.0.7'
5
5
  end
@@ -3,6 +3,7 @@
3
3
  module LightSerializer
4
4
  end
5
5
 
6
+ require 'oj'
6
7
  require 'light_serializer/helpers/with_custom_root'
7
8
  require 'light_serializer/hashed_object'
8
9
  require 'light_serializer/serializer'
@@ -0,0 +1,353 @@
1
+ ## Comparison:
2
+ * LightSerializer
3
+ * AMS
4
+ * Surrealist
5
+ * Blueprinter
6
+
7
+ All tests were made on MB Pro 2015 15".
8
+
9
+ ```
10
+ Warming up --------------------------------------
11
+ AMS : instance 2.225k i/100ms
12
+ Surrealist: instance through .surrealize
13
+ 4.012k i/100ms
14
+ LightSerializer: instance through .to_json
15
+ 8.733k i/100ms
16
+ Surrealist: instance through Surrealist::Serializer
17
+ 2.854k i/100ms
18
+ ActiveModel::Serializers::JSON instance
19
+ 2.404k i/100ms
20
+ Blueprinter 4.747k i/100ms
21
+ Calculating -------------------------------------
22
+ AMS : instance 23.170k (± 3.1%) i/s - 117.925k in 5.094753s
23
+ Surrealist: instance through .surrealize
24
+ 41.814k (± 2.6%) i/s - 212.636k in 5.089193s
25
+ LightSerializer: instance through .to_json
26
+ 91.327k (± 2.5%) i/s - 462.849k in 5.071370s
27
+ Surrealist: instance through Surrealist::Serializer
28
+ 29.131k (± 2.8%) i/s - 145.554k in 5.000956s
29
+ ActiveModel::Serializers::JSON instance
30
+ 24.605k (± 2.0%) i/s - 125.008k in 5.082821s
31
+ Blueprinter 49.770k (± 3.3%) i/s - 251.591k in 5.061158s
32
+
33
+ Comparison:
34
+ LightSerializer: instance through .to_json: 91327.3 i/s
35
+ Blueprinter: 49770.4 i/s - 1.83x slower
36
+ Surrealist: instance through .surrealize: 41813.5 i/s - 2.18x slower
37
+ Surrealist: instance through Surrealist::Serializer: 29130.9 i/s - 3.14x slower
38
+ ActiveModel::Serializers::JSON instance: 24604.5 i/s - 3.71x slower
39
+ AMS : instance: 23169.7 i/s - 3.94x slower
40
+
41
+ Warming up --------------------------------------
42
+ AMS : collection 1.000 i/100ms
43
+ Surrealist: collection through Surrealist.surrealize_collection()
44
+ 1.000 i/100ms
45
+ LightSerializer: collection through .to_json
46
+ 1.000 i/100ms
47
+ Surrealist: collection through Surrealist::Serializer
48
+ 1.000 i/100ms
49
+ ActiveModel::Serializers::JSON collection
50
+ 1.000 i/100ms
51
+ Blueprinter collection
52
+ 1.000 i/100ms
53
+ Calculating -------------------------------------
54
+ AMS : collection 5.801 (± 0.0%) i/s - 29.000 in 5.004853s
55
+ Surrealist: collection through Surrealist.surrealize_collection()
56
+ 9.691 (± 0.0%) i/s - 49.000 in 5.063183s
57
+ LightSerializer: collection through .to_json
58
+ 19.861 (± 5.0%) i/s - 100.000 in 5.044340s
59
+ Surrealist: collection through Surrealist::Serializer
60
+ 7.964 (± 0.0%) i/s - 40.000 in 5.029143s
61
+ ActiveModel::Serializers::JSON collection
62
+ 6.601 (± 0.0%) i/s - 33.000 in 5.002701s
63
+ Blueprinter collection
64
+ 18.376 (± 5.4%) i/s - 92.000 in 5.015767s
65
+
66
+ Comparison:
67
+ LightSerializer: collection through .to_json: 19.9 i/s
68
+ Blueprinter collection: 18.4 i/s - same-ish: difference falls within error
69
+ Surrealist: collection through Surrealist.surrealize_collection(): 9.7 i/s - 2.05x slower
70
+ Surrealist: collection through Surrealist::Serializer: 8.0 i/s - 2.49x slower
71
+ ActiveModel::Serializers::JSON collection: 6.6 i/s - 3.01x slower
72
+ AMS : collection: 5.8 i/s - 3.42x slower
73
+
74
+
75
+ ------- Turning off AMS logger -------
76
+ Warming up --------------------------------------
77
+ AMS(without logging) : instance
78
+ 2.554k i/100ms
79
+ Surrealist: instance through .surrealize
80
+ 4.451k i/100ms
81
+ LightSerializer: instance through .to_json
82
+ 9.498k i/100ms
83
+ Surrealist: instance through Surrealist::Serializer
84
+ 3.090k i/100ms
85
+ ActiveModel::Serializers::JSON instance
86
+ 2.683k i/100ms
87
+ Blueprinter 5.318k i/100ms
88
+ Calculating -------------------------------------
89
+ AMS(without logging) : instance
90
+ 25.965k (± 3.8%) i/s - 130.254k in 5.024395s
91
+ Surrealist: instance through .surrealize
92
+ 45.524k (± 4.9%) i/s - 231.452k in 5.097901s
93
+ LightSerializer: instance through .to_json
94
+ 99.406k (± 2.8%) i/s - 503.394k in 5.067891s
95
+ Surrealist: instance through Surrealist::Serializer
96
+ 32.110k (± 2.9%) i/s - 160.680k in 5.008350s
97
+ ActiveModel::Serializers::JSON instance
98
+ 27.232k (± 2.8%) i/s - 136.833k in 5.028844s
99
+ Blueprinter 54.973k (± 2.7%) i/s - 276.536k in 5.033870s
100
+
101
+ Comparison:
102
+ LightSerializer: instance through .to_json: 99406.0 i/s
103
+ Blueprinter: 54973.4 i/s - 1.81x slower
104
+ Surrealist: instance through .surrealize: 45523.7 i/s - 2.18x slower
105
+ Surrealist: instance through Surrealist::Serializer: 32110.0 i/s - 3.10x slower
106
+ ActiveModel::Serializers::JSON instance: 27232.1 i/s - 3.65x slower
107
+ AMS(without logging) : instance: 25965.2 i/s - 3.83x slower
108
+
109
+ Warming up --------------------------------------
110
+ AMS(without logging) : collection
111
+ 1.000 i/100ms
112
+ Surrealist: collection through Surrealist.surrealize_collection()
113
+ 1.000 i/100ms
114
+ LightSerializer: collection through .to_json
115
+ 1.000 i/100ms
116
+ Surrealist: collection through Surrealist::Serializer
117
+ 1.000 i/100ms
118
+ ActiveModel::Serializers::JSON collection
119
+ 1.000 i/100ms
120
+ Blueprinter collection
121
+ 1.000 i/100ms
122
+ Calculating -------------------------------------
123
+ AMS(without logging) : collection
124
+ 5.729 (± 0.0%) i/s - 29.000 in 5.065254s
125
+ Surrealist: collection through Surrealist.surrealize_collection()
126
+ 9.684 (± 0.0%) i/s - 49.000 in 5.063764s
127
+ LightSerializer: collection through .to_json
128
+ 19.538 (± 5.1%) i/s - 98.000 in 5.026798s
129
+ Surrealist: collection through Surrealist::Serializer
130
+ 7.836 (± 0.0%) i/s - 40.000 in 5.110566s
131
+ ActiveModel::Serializers::JSON collection
132
+ 6.518 (± 0.0%) i/s - 33.000 in 5.067046s
133
+ Blueprinter collection
134
+ 18.106 (± 5.5%) i/s - 91.000 in 5.034963s
135
+
136
+ Comparison:
137
+ LightSerializer: collection through .to_json: 19.5 i/s
138
+ Blueprinter collection: 18.1 i/s - same-ish: difference falls within error
139
+ Surrealist: collection through Surrealist.surrealize_collection(): 9.7 i/s - 2.02x slower
140
+ Surrealist: collection through Surrealist::Serializer: 7.8 i/s - 2.49x slower
141
+ ActiveModel::Serializers::JSON collection: 6.5 i/s - 3.00x slower
142
+ AMS(without logging) : collection: 5.7 i/s - 3.41x slower
143
+
144
+ Warming up --------------------------------------
145
+ AMS (associations): instance
146
+ 338.000 i/100ms
147
+ Surrealist (associations): instance through .surrealize
148
+ 907.000 i/100ms
149
+ Light (associations): instance through .to_json
150
+ 2.627k i/100ms
151
+ Surrealist (associations): instance through Surrealist::Serializer
152
+ 921.000 i/100ms
153
+ ActiveModel::Serializers::JSON (associations)
154
+ 1.024k i/100ms
155
+ Blueprinter (associations)
156
+ 2.029k i/100ms
157
+ Calculating -------------------------------------
158
+ AMS (associations): instance
159
+ 3.371k (± 2.9%) i/s - 16.900k in 5.018308s
160
+ Surrealist (associations): instance through .surrealize
161
+ 9.139k (± 2.9%) i/s - 46.257k in 5.065403s
162
+ Light (associations): instance through .to_json
163
+ 26.902k (± 2.8%) i/s - 136.604k in 5.081821s
164
+ Surrealist (associations): instance through Surrealist::Serializer
165
+ 9.286k (± 3.0%) i/s - 46.971k in 5.062770s
166
+ ActiveModel::Serializers::JSON (associations)
167
+ 10.313k (± 2.4%) i/s - 52.224k in 5.066930s
168
+ Blueprinter (associations)
169
+ 20.826k (± 3.4%) i/s - 105.508k in 5.071876s
170
+
171
+ Comparison:
172
+ Light (associations): instance through .to_json: 26902.2 i/s
173
+ Blueprinter (associations): 20825.6 i/s - 1.29x slower
174
+ ActiveModel::Serializers::JSON (associations): 10312.9 i/s - 2.61x slower
175
+ Surrealist (associations): instance through Surrealist::Serializer: 9286.0 i/s - 2.90x slower
176
+ Surrealist (associations): instance through .surrealize: 9139.5 i/s - 2.94x slower
177
+ AMS (associations): instance: 3370.6 i/s - 7.98x slower
178
+
179
+ Warming up --------------------------------------
180
+ AMS (associations): collection
181
+ 1.000 i/100ms
182
+ Surrealist (associations): collection through Surrealist.surrealize_collection()
183
+ 1.000 i/100ms
184
+ Light (associations): collection through .to_json
185
+ 1.000 i/100ms
186
+ Surrealist (associations): collection through Surrealist::Serializer
187
+ 1.000 i/100ms
188
+ ActiveModel::Serializers::JSON (associations): collection
189
+ 1.000 i/100ms
190
+ Blueprinter (associations): collection
191
+ 1.000 i/100ms
192
+ Calculating -------------------------------------
193
+ AMS (associations): collection
194
+ 1.172 (± 0.0%) i/s - 6.000 in 5.138824s
195
+ Surrealist (associations): collection through Surrealist.surrealize_collection()
196
+ 2.694 (± 0.0%) i/s - 14.000 in 5.203211s
197
+ Light (associations): collection through .to_json
198
+ 8.001 (± 0.0%) i/s - 40.000 in 5.008966s
199
+ Surrealist (associations): collection through Surrealist::Serializer
200
+ 2.635 (± 0.0%) i/s - 14.000 in 5.319793s
201
+ ActiveModel::Serializers::JSON (associations): collection
202
+ 3.309 (± 0.0%) i/s - 17.000 in 5.144606s
203
+ Blueprinter (associations): collection
204
+ 7.817 (± 0.0%) i/s - 40.000 in 5.128954s
205
+
206
+ Comparison:
207
+ Light (associations): collection through .to_json: 8.0 i/s
208
+ Blueprinter (associations): collection: 7.8 i/s - 1.02x slower
209
+ ActiveModel::Serializers::JSON (associations): collection: 3.3 i/s - 2.42x slower
210
+ Surrealist (associations): collection through Surrealist.surrealize_collection(): 2.7 i/s - 2.97x slower
211
+ Surrealist (associations): collection through Surrealist::Serializer: 2.6 i/s - 3.04x slower
212
+ AMS (associations): collection: 1.2 i/s - 6.83x slower
213
+
214
+
215
+ ------- Enabling Oj.optimize_rails() & Blueprinter config.generator = Oj -------
216
+ Warming up --------------------------------------
217
+ AMS(without logging) (with Oj): instance
218
+ 4.286k i/100ms
219
+ Surrealist: instance through .surrealize
220
+ 4.466k i/100ms
221
+ LightSerializer: instance through .to_json
222
+ 9.569k i/100ms
223
+ Surrealist: instance through Surrealist::Serializer
224
+ 3.124k i/100ms
225
+ ActiveModel::Serializers::JSON(with Oj) instance
226
+ 4.035k i/100ms
227
+ Blueprinter(with Oj) 6.773k i/100ms
228
+ Calculating -------------------------------------
229
+ AMS(without logging) (with Oj): instance
230
+ 49.629k (± 4.8%) i/s - 248.588k in 5.020623s
231
+ Surrealist: instance through .surrealize
232
+ 45.766k (± 3.2%) i/s - 232.232k in 5.079519s
233
+ LightSerializer: instance through .to_json
234
+ 99.043k (± 2.6%) i/s - 497.588k in 5.027351s
235
+ Surrealist: instance through Surrealist::Serializer
236
+ 31.665k (± 3.4%) i/s - 159.324k in 5.037471s
237
+ ActiveModel::Serializers::JSON(with Oj) instance
238
+ 45.115k (± 4.0%) i/s - 225.960k in 5.017009s
239
+ Blueprinter(with Oj) 70.319k (± 2.5%) i/s - 352.196k in 5.011834s
240
+
241
+ Comparison:
242
+ LightSerializer: instance through .to_json: 99042.9 i/s
243
+ Blueprinter(with Oj): 70319.0 i/s - 1.41x slower
244
+ AMS(without logging) (with Oj): instance: 49629.4 i/s - 2.00x slower
245
+ Surrealist: instance through .surrealize: 45766.5 i/s - 2.16x slower
246
+ ActiveModel::Serializers::JSON(with Oj) instance: 45114.9 i/s - 2.20x slower
247
+ Surrealist: instance through Surrealist::Serializer: 31665.1 i/s - 3.13x slower
248
+
249
+ Warming up --------------------------------------
250
+ AMS(without logging) (with Oj): collection
251
+ 1.000 i/100ms
252
+ Surrealist: collection through Surrealist.surrealize_collection()
253
+ 1.000 i/100ms
254
+ LightSerializer: collection through .to_json
255
+ 1.000 i/100ms
256
+ Surrealist: collection through Surrealist::Serializer
257
+ 1.000 i/100ms
258
+ ActiveModel::Serializers::JSON(with Oj) collection
259
+ 1.000 i/100ms
260
+ Blueprinter collection(with Oj)
261
+ 2.000 i/100ms
262
+ Calculating -------------------------------------
263
+ AMS(without logging) (with Oj): collection
264
+ 9.921 (± 0.0%) i/s - 50.000 in 5.047071s
265
+ Surrealist: collection through Surrealist.surrealize_collection()
266
+ 9.459 (± 0.0%) i/s - 48.000 in 5.080945s
267
+ LightSerializer: collection through .to_json
268
+ 19.470 (± 5.1%) i/s - 98.000 in 5.046335s
269
+ Surrealist: collection through Surrealist::Serializer
270
+ 7.720 (± 0.0%) i/s - 39.000 in 5.063697s
271
+ ActiveModel::Serializers::JSON(with Oj) collection
272
+ 10.796 (± 9.3%) i/s - 54.000 in 5.024268s
273
+ Blueprinter collection(with Oj)
274
+ 20.156 (± 5.0%) i/s - 102.000 in 5.064738s
275
+
276
+ Comparison:
277
+ Blueprinter collection(with Oj): 20.2 i/s
278
+ LightSerializer: collection through .to_json: 19.5 i/s - same-ish: difference falls within error
279
+ ActiveModel::Serializers::JSON(with Oj) collection: 10.8 i/s - 1.87x slower
280
+ AMS(without logging) (with Oj): collection: 9.9 i/s - 2.03x slower
281
+ Surrealist: collection through Surrealist.surrealize_collection(): 9.5 i/s - 2.13x slower
282
+ Surrealist: collection through Surrealist::Serializer: 7.7 i/s - 2.61x slower
283
+
284
+ Warming up --------------------------------------
285
+ AMS (associations): instance
286
+ 381.000 i/100ms
287
+ Surrealist (associations): instance through .surrealize
288
+ 677.000 i/100ms
289
+ Light (associations): instance through .to_json
290
+ 2.074k i/100ms
291
+ Surrealist (associations): instance through Surrealist::Serializer
292
+ 687.000 i/100ms
293
+ ActiveModel::Serializers::JSON (associations)
294
+ 1.390k i/100ms
295
+ Blueprinter (associations)
296
+ 1.851k i/100ms
297
+ Calculating -------------------------------------
298
+ AMS (associations): instance
299
+ 3.743k (± 5.9%) i/s - 19.050k in 5.108122s
300
+ Surrealist (associations): instance through .surrealize
301
+ 6.817k (± 2.8%) i/s - 34.527k in 5.068641s
302
+ Light (associations): instance through .to_json
303
+ 21.131k (± 3.0%) i/s - 105.774k in 5.010100s
304
+ Surrealist (associations): instance through Surrealist::Serializer
305
+ 6.874k (± 3.1%) i/s - 34.350k in 5.001779s
306
+ ActiveModel::Serializers::JSON (associations)
307
+ 13.996k (± 3.2%) i/s - 70.890k in 5.069785s
308
+ Blueprinter (associations)
309
+ 19.964k (± 3.5%) i/s - 99.954k in 5.013021s
310
+
311
+ Comparison:
312
+ Light (associations): instance through .to_json: 21131.3 i/s
313
+ Blueprinter (associations): 19964.2 i/s - same-ish: difference falls within error
314
+ ActiveModel::Serializers::JSON (associations): 13996.4 i/s - 1.51x slower
315
+ Surrealist (associations): instance through Surrealist::Serializer: 6874.1 i/s - 3.07x slower
316
+ Surrealist (associations): instance through .surrealize: 6817.0 i/s - 3.10x slower
317
+ AMS (associations): instance: 3742.8 i/s - 5.65x slower
318
+
319
+ Warming up --------------------------------------
320
+ AMS (associations): collection
321
+ 1.000 i/100ms
322
+ Surrealist (associations): collection through Surrealist.surrealize_collection()
323
+ 1.000 i/100ms
324
+ Light (associations): collection through .to_json
325
+ 1.000 i/100ms
326
+ Surrealist (associations): collection through Surrealist::Serializer
327
+ 1.000 i/100ms
328
+ ActiveModel::Serializers::JSON (associations): collection
329
+ 1.000 i/100ms
330
+ Blueprinter (associations): collection
331
+ 1.000 i/100ms
332
+ Calculating -------------------------------------
333
+ AMS (associations): collection
334
+ 1.387 (± 0.0%) i/s - 7.000 in 5.077429s
335
+ Surrealist (associations): collection through Surrealist.surrealize_collection()
336
+ 2.649 (± 0.0%) i/s - 14.000 in 5.291537s
337
+ Light (associations): collection through .to_json
338
+ 7.857 (± 0.0%) i/s - 40.000 in 5.103876s
339
+ Surrealist (associations): collection through Surrealist::Serializer
340
+ 2.602 (± 0.0%) i/s - 13.000 in 5.003615s
341
+ ActiveModel::Serializers::JSON (associations): collection
342
+ 5.693 (± 0.0%) i/s - 29.000 in 5.104138s
343
+ Blueprinter (associations): collection
344
+ 8.603 (± 0.0%) i/s - 43.000 in 5.011885s
345
+
346
+ Comparison:
347
+ Blueprinter (associations): collection: 8.6 i/s
348
+ Light (associations): collection through .to_json: 7.9 i/s - 1.09x slower
349
+ ActiveModel::Serializers::JSON (associations): collection: 5.7 i/s - 1.51x slower
350
+ Surrealist (associations): collection through Surrealist.surrealize_collection(): 2.6 i/s - 3.25x slower
351
+ Surrealist (associations): collection through Surrealist::Serializer: 2.6 i/s - 3.31x slower
352
+ AMS (associations): collection: 1.4 i/s - 6.20x slower
353
+ ```
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Rodionov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-09 00:00:00.000000000 Z
12
+ date: 2018-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oj
@@ -162,6 +162,7 @@ files:
162
162
  - lib/light_serializer/serializer.rb
163
163
  - lib/light_serializer/version.rb
164
164
  - light_serializer.gemspec
165
+ - performance_comparison.md
165
166
  homepage: https://github.com/krim/light_serializer
166
167
  licenses:
167
168
  - MIT