Skip to content

Commit 09f9917

Browse files
committed
Enhance performance
1 parent a73b483 commit 09f9917

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

lib/json/ld/flatten.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ def rename_bnodes(node)
255255
when Array
256256
node.map { |n| rename_bnodes(n) }
257257
when Hash
258-
node.inject({}) do |memo, (k, v)|
258+
node.each_with_object({}) do |(k, v), memo|
259259
v = namer.get_name(v) if k == '@id' && v.is_a?(String) && blank_node?(v)
260-
memo.merge(k => rename_bnodes(v))
260+
memo[k] = rename_bnodes(v)
261261
end
262262
else
263263
node

lib/json/ld/frame.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,14 @@ def prune_bnodes(input, bnodes_to_clear)
273273
def cleanup_preserve(input)
274274
case input
275275
when Array
276-
# If, after replacement, an array contains only the value null remove the value, leaving an empty array.
277-
input.map { |o| cleanup_preserve(o) }
276+
input.map! { |o| cleanup_preserve(o) }
278277
when Hash
279278
if input.key?('@preserve')
280279
# Replace with the content of `@preserve`
281280
cleanup_preserve(input['@preserve'].first)
282281
else
283-
input.inject({}) do |memo, (k, v)|
284-
memo.merge(k => cleanup_preserve(v))
282+
input.transform_values do |v|
283+
cleanup_preserve(v)
285284
end
286285
end
287286
else
@@ -298,10 +297,10 @@ def cleanup_null(input)
298297
case input
299298
when Array
300299
# If, after replacement, an array contains only the value null remove the value, leaving an empty array.
301-
input.map { |o| cleanup_null(o) }.compact
300+
input.map! { |o| cleanup_null(o) }.compact
302301
when Hash
303-
input.inject({}) do |memo, (k, v)|
304-
memo.merge(k => cleanup_null(v))
302+
input.transform_values do |v|
303+
cleanup_null(v)
305304
end
306305
when '@null'
307306
# If the value from the key-pair is @null, replace the value with null

0 commit comments

Comments
 (0)