Skip to content

Commit d12a153

Browse files
committed
Refactor .reject.each and .reject.map to not allocate temporary array.
1 parent 58d95cc commit d12a153

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

lib/json/ld/api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ def self.flatten(input, context, options = {})
277277
create_node_map(value, graph_maps)
278278

279279
default_graph = graph_maps['@default']
280-
graph_maps.keys.kw_sort.reject {|k| k == '@default'}.each do |graph_name|
280+
graph_maps.keys.kw_sort.each do |graph_name|
281+
next if graph_name == '@default'
282+
281283
graph = graph_maps[graph_name]
282284
entry = default_graph[graph_name] ||= {'@id' => graph_name}
283285
nodes = entry['@graph'] ||= []

lib/json/ld/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ def from_vocabulary(graph)
800800
# Add term definitions for each class and property not in vocab, and
801801
# for those properties having an object range
802802
statements.each do |subject, values|
803-
types = values.select {|v| v.predicate == RDF.type}.map(&:object)
803+
types = values.each_with_object([]) { |v, memo| memo << v.object if v.predicate == RDF.type }
804804
is_property = types.any? {|t| t.to_s.include?("Property")}
805805

806806
term = subject.to_s.split(/[\/\#]/).last

lib/json/ld/frame.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ def frame(state, subjects, frame, **options)
154154
end
155155

156156
# handle defaults in order
157-
frame.keys.kw_sort.reject {|p| p.start_with?('@')}.each do |prop|
157+
frame.keys.kw_sort.each do |prop|
158+
next if prop.start_with?('@')
159+
158160
# if omit default is off, then include default values for properties that appear in the next frame but are not in the matching subject
159161
n = frame[prop].first || {}
160162
omit_default_on = get_frame_flag(n, options, :omitDefault)

0 commit comments

Comments
 (0)