Skip to content

Commit b8abc01

Browse files
jpervillegkellogg
authored andcommitted
Reduce object allocations in JSON::LD::Compact#compact
1 parent 7ad3a67 commit b8abc01

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

lib/json/ld/compact.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module JSON::LD
44
module Compact
55
include Utils
66

7+
# The following constant is used to reduce object allocations in #compact below
8+
CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE = Set.new(%w(@language @index @id @type)).freeze
9+
710
##
811
# This algorithm compacts a JSON-LD document, such that the given context is applied. This must result in shortening any applicable IRIs to terms or compact IRIs, any applicable keywords to keyword aliases, and any applicable JSON-LD values expressed in expanded form to simple values such as strings or numbers.
912
#
@@ -234,7 +237,7 @@ def compact(element, property: nil, ordered: false)
234237
add_value(nest_result, item_active_property, compacted_item,
235238
property_is_array: as_array)
236239
end
237-
elsif !(container & %w(@language @index @id @type)).empty? && !container.include?('@graph')
240+
elsif container.any? { |key| CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE.include?(key) } && !container.include?('@graph')
238241
map_object = nest_result[item_active_property] ||= {}
239242
c = container.first
240243
container_key = context.compact_iri(c, vocab: true, quiet: true)

0 commit comments

Comments
 (0)