Skip to content

Commit dc18d7f

Browse files
authored
Remove deep cloning of contexts during parsing
This improves overall parsing performance
1 parent 5a23c1f commit dc18d7f

File tree

5 files changed

+12
-19
lines changed

5 files changed

+12
-19
lines changed

lib/ParsingContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class ParsingContext {
207207
|| scopedContext[key]['@context']['@propagate']; // Propagation is true by default
208208

209209
if (propagate !== false || i === keysOriginal.length - 1 - offset) {
210-
contextRaw = scopedContext;
210+
contextRaw = { ...scopedContext };
211211

212212
// Clean up final context
213213
delete contextRaw['@propagate'];

lib/entryhandler/keyword/EntryHandlerKeywordType.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,11 @@ export class EntryHandlerKeywordType extends EntryHandlerKeyword {
6969
if (hasTypedScopedContext) {
7070
// Do not propagate by default
7171
scopedContext = scopedContext.then((c) => {
72-
if (!('@propagate' in c.getContextRaw())) {
73-
c.getContextRaw()['@propagate'] = false;
74-
}
75-
7672
// Set the original context at this depth as a fallback
7773
// This is needed when a context was already defined at the given depth,
7874
// and this context needs to remain accessible from child nodes when propagation is disabled.
79-
if (c.getContextRaw()['@propagate'] === false) {
80-
c.getContextRaw()['@__propagateFallback'] = context.getContextRaw();
75+
if (c.getContextRaw()['@propagate'] !== true) {
76+
return new JsonLdContextNormalized({ ...c.getContextRaw(), '@propagate': false, '@__propagateFallback': context.getContextRaw() });
8177
}
8278

8379
return c;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"buffer": "^6.0.3",
3939
"canonicalize": "^1.0.1",
4040
"http-link-header": "^1.0.2",
41-
"jsonld-context-parser": "^2.3.3",
41+
"jsonld-context-parser": "^2.4.0",
4242
"rdf-data-factory": "^1.1.0",
4343
"readable-stream": "^4.0.0"
4444
},

test/ParsingContext-test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {JsonLdContextNormalized} from "jsonld-context-parser";
1+
import {JsonLdContextNormalized, ContextParser} from "jsonld-context-parser";
22
import {ParsingContext} from "../lib/ParsingContext";
33
import {ParsingContextMocked} from "../mocks/ParsingContextMocked";
44

@@ -607,22 +607,20 @@ describe('ParsingContext', () => {
607607
describe('for type-scoped and property-scoped contexts', () => {
608608

609609
beforeEach(() => {
610-
parsingContext.contextTree.setContext(['', 'a'], Promise.resolve(new JsonLdContextNormalized({
611-
'@__propagateFallback': { fallback: true },
612-
'@propagate': false,
610+
const contextParser = new ContextParser();
611+
parsingContext.contextTree.setContext(['', 'a'], contextParser.parse({
613612
'@vocab': 'http://bla.org/',
614613
'bar': {
615614
'@context': {
616615
baz: { '@type': '@vocab' },
617616
},
618617
},
619-
})));
618+
}));
620619
});
621620

622621
it('should consider the applicable property-scoped context', async () => {
623622
return expect(await parsingContext.getContext(['', 'a', 'bar'], 0))
624623
.toEqual(new JsonLdContextNormalized({
625-
'@__propagateFallback': { fallback: true },
626624
'@vocab': 'http://bla.org/',
627625
'bar': { '@id': 'http://bla.org/bar' },
628626
'baz': { '@type': '@vocab', '@id': 'http://bla.org/baz' },

yarn.lock

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,14 +2518,13 @@ jsonld-context-parser@^2.0.2, jsonld-context-parser@^2.1.3:
25182518
http-link-header "^1.0.2"
25192519
relative-to-absolute-iri "^1.0.5"
25202520

2521-
jsonld-context-parser@^2.3.3:
2522-
version "2.3.3"
2523-
resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.3.3.tgz#0bdab9eb5cb4b7e68aa7d6c38e58455363caaf9c"
2524-
integrity sha512-H+REInOx7XI2ciF8wJV31D20Bh+ofBmEjN2Tkds51vypqDJIiD341E5g+hYyrEInIKRnbW58TN/Ehz+ACT0l0w==
2521+
jsonld-context-parser@^2.4.0:
2522+
version "2.4.0"
2523+
resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.4.0.tgz#fae15a56c5ceabd1c4520ab1a9cc12c9a0a8b67d"
2524+
integrity sha512-ZYOfvh525SdPd9ReYY58dxB3E2RUEU4DJ6ZibO8AitcowPeBH4L5rCAitE2om5G1P+HMEgYEYEr4EZKbVN4tpA==
25252525
dependencies:
25262526
"@types/http-link-header" "^1.0.1"
25272527
"@types/node" "^18.0.0"
2528-
canonicalize "^1.0.1"
25292528
cross-fetch "^3.0.6"
25302529
http-link-header "^1.0.2"
25312530
relative-to-absolute-iri "^1.0.5"

0 commit comments

Comments
 (0)