mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Prefer the faster and more idiomatic Array#map in json_clone (#4313)
* Prefer the faster Array#map to a manual implementation * On the benchmark from https://github.com/mperham/sidekiq/pull/4303 on MRI 2.6.4, before 396657.9 i/s, after 461013.3 i/s. * Use `duped` as the return value in the only branch it's needed
This commit is contained in:
parent
b138213d4d
commit
3380c12af9
1 changed files with 5 additions and 9 deletions
|
@ -279,24 +279,20 @@ module Sidekiq
|
||||||
# been mutated by the worker.
|
# been mutated by the worker.
|
||||||
def json_clone(obj)
|
def json_clone(obj)
|
||||||
if Integer === obj || Float === obj || TrueClass === obj || FalseClass === obj || NilClass === obj
|
if Integer === obj || Float === obj || TrueClass === obj || FalseClass === obj || NilClass === obj
|
||||||
return obj
|
obj
|
||||||
elsif String === obj
|
elsif String === obj
|
||||||
return obj.dup
|
obj.dup
|
||||||
elsif Array === obj
|
elsif Array === obj
|
||||||
duped = Array.new(obj.size)
|
obj.map { |e| json_clone(e) }
|
||||||
obj.each_with_index do |value, index|
|
|
||||||
duped[index] = json_clone(value)
|
|
||||||
end
|
|
||||||
elsif Hash === obj
|
elsif Hash === obj
|
||||||
duped = obj.dup
|
duped = obj.dup
|
||||||
duped.each_pair do |key, value|
|
duped.each_pair do |key, value|
|
||||||
duped[key] = json_clone(value)
|
duped[key] = json_clone(value)
|
||||||
end
|
end
|
||||||
|
duped
|
||||||
else
|
else
|
||||||
duped = obj.dup
|
obj.dup
|
||||||
end
|
end
|
||||||
|
|
||||||
duped
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue