1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reduce number of created objects in Hash#to_json

Followup to 9256ae8a38.

Since the result of `as_json` is typically encoded with `to_json`, this
patch should save the same number of allocations as the previous one.
This commit is contained in:
Eugene Kenny 2020-01-11 11:03:20 +00:00
parent c2d7bdc6cc
commit 144df5b104

View file

@ -93,7 +93,11 @@ module ActiveSupport
when Numeric, NilClass, TrueClass, FalseClass
value.as_json
when Hash
Hash[value.map { |k, v| [jsonify(k), jsonify(v)] }]
result = {}
value.each do |k, v|
result[jsonify(k)] = jsonify(v)
end
result
when Array
value.map { |v| jsonify(v) }
else