Skip to content

Commit 16c99bd

Browse files
dlongleydavidlehn
authored andcommitted
Use Set to manage cycles.
1 parent 8696a0b commit 16c99bd

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/context.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -946,13 +946,13 @@ async function _retrieveContextUrls(input, options) {
946946
const documentLoader = util.normalizeDocumentLoader(options.documentLoader);
947947

948948
// retrieve all @context URLs in input
949-
await retrieve(input, {}, documentLoader);
949+
await retrieve(input, new Set(), documentLoader);
950950

951951
return input;
952952

953953
// recursive function that will retrieve all @context URLs in documents
954954
async function retrieve(doc, cycles, documentLoader) {
955-
if(Object.keys(cycles).length > MAX_CONTEXT_URLS) {
955+
if(cycles.size > MAX_CONTEXT_URLS) {
956956
throw new JsonLdError(
957957
'Maximum number of @context URLs exceeded.',
958958
'jsonld.ContextUrlError',
@@ -969,15 +969,15 @@ async function _retrieveContextUrls(input, options) {
969969
// retrieve URLs in queue
970970
return Promise.all(queue.map(async url => {
971971
// check for context URL cycle
972-
if(url in cycles) {
972+
if(cycles.has(url)) {
973973
throw new JsonLdError(
974974
'Cyclical @context URLs detected.',
975975
'jsonld.ContextUrlError',
976976
{code: 'recursive context inclusion', url});
977977
}
978978

979-
const _cycles = util.clone(cycles);
980-
_cycles[url] = true;
979+
const _cycles = new Set(cycles);
980+
_cycles.add(url);
981981
let remoteDoc;
982982
let ctx;
983983

0 commit comments

Comments
 (0)