Skip to content

Commit bce9e05

Browse files
committed
perf: reduce redefinition iterations
1 parent 1602365 commit bce9e05

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/ContextParser.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,19 @@ Tried mapping ${key} to ${JSON.stringify(keyValue)}`, ERROR_CODES.INVALID_KEYWOR
312312
expandOptions: IExpandOptions,
313313
/* istanbul ignore next */
314314
keys = Object.keys(contextAfter)) {
315-
for (const key of Object.keys(contextAfter)) {
315+
for (const key of keys) {
316316
if (Util.isTermProtected(contextBefore, key)) {
317317
// The entry in the context before will always be in object-mode
318318
// If the new entry is in string-mode, convert it to object-mode
319319
// before checking if it is identical.
320320
if (typeof contextAfter[key] === 'string') {
321-
contextAfter[key] = { '@id': contextAfter[key] };
322-
}
321+
contextAfter[key] = { '@id': contextAfter[key], '@protected': true };
322+
} else {
323323
// We modify this deliberately,
324324
// as we need it for the value comparison (they must be identical modulo '@protected')),
325325
// and for the fact that this new value will override the first one.
326-
contextAfter[key] = {...contextAfter[key], '@protected': true};
326+
contextAfter[key] = {...contextAfter[key], '@protected': true};
327+
}
327328

328329
// Error if they are not identical
329330
if (!deepEqual(contextBefore[key], contextAfter[key])) {
@@ -794,10 +795,14 @@ must be one of ${Util.CONTAINERS.join(', ')}`, ERROR_CODES.INVALID_CONTAINER_MAP
794795
this.applyScopedProtected(newContext, { processingMode }, defaultExpandOptions);
795796

796797
const keys = Object.keys(newContext);
798+
799+
const overlappingKeys: string[] = [];
797800
if (typeof parentContext === 'object') {
798801
// Merge different parts of the final context in order
799802
for (const key in parentContext) {
800-
if (!(key in newContext)) {
803+
if (key in newContext) {
804+
overlappingKeys.push(key);
805+
} else {
801806
newContext[key] = parentContext[key];
802807
}
803808
}
@@ -825,7 +830,7 @@ must be one of ${Util.CONTAINERS.join(', ')}`, ERROR_CODES.INVALID_CONTAINER_MAP
825830

826831
// In JSON-LD 1.1, check if we are not redefining any protected keywords
827832
if (!ignoreProtection && parentContext && processingMode >= 1.1) {
828-
this.validateKeywordRedefinitions(parentContext, newContext, defaultExpandOptions, keys);
833+
this.validateKeywordRedefinitions(parentContext, newContext, defaultExpandOptions, overlappingKeys);
829834
}
830835

831836
if (this.validateContext && !ioptions.skipValidation) {

0 commit comments

Comments
 (0)