Skip to content

Commit 55dee7e

Browse files
committed
Perf of dereference is unstable
1 parent d364d51 commit 55dee7e

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

lib/dereference.js

+35-40
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = dereference;
1616
*/
1717
function dereference (parser, options) {
1818
// console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
19-
let dereferenced = crawl(parser.schema, parser.$refs._root$Ref.path, "#", [], [], {}, parser.$refs, options);
19+
let dereferenced = crawl(parser.schema, parser.$refs._root$Ref.path, "#", [], {}, parser.$refs, options);
2020
parser.$refs.circular = dereferenced.circular;
2121
parser.schema = dereferenced.value;
2222
}
@@ -28,65 +28,61 @@ function dereference (parser, options) {
2828
* @param {string} path - The full path of `obj`, possibly with a JSON Pointer in the hash
2929
* @param {string} pathFromRoot - The path of `obj` from the schema root
3030
* @param {object[]} parents - An array of the parent objects that have already been dereferenced
31-
* @param {object[]} processedObjects - An array of all the objects that have already been processed
3231
* @param {object} dereferencedCache - An map of all the dereferenced objects
3332
* @param {$Refs} $refs
3433
* @param {$RefParserOptions} options
3534
* @returns {{value: object, circular: boolean}}
3635
*/
37-
function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) {
36+
function crawl (obj, path, pathFromRoot, parents, dereferencedCache, $refs, options) {
3837
let dereferenced;
3938
let result = {
4039
value: obj,
4140
circular: false
4241
};
4342

44-
if (options.dereference.circular === "ignore" || processedObjects.indexOf(obj) === -1) {
45-
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) {
46-
parents.push(obj);
47-
processedObjects.push(obj);
43+
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) {
44+
parents.push(obj);
4845

49-
if ($Ref.isAllowed$Ref(obj, options)) {
50-
dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
51-
result.circular = dereferenced.circular;
52-
result.value = dereferenced.value;
53-
}
54-
else {
55-
for (let key of Object.keys(obj)) {
56-
let keyPath = Pointer.join(path, key);
57-
let keyPathFromRoot = Pointer.join(pathFromRoot, key);
58-
let value = obj[key];
59-
let circular = false;
60-
61-
if ($Ref.isAllowed$Ref(value, options)) {
62-
dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
46+
if ($Ref.isAllowed$Ref(obj, options)) {
47+
dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, dereferencedCache, $refs, options);
48+
result.circular = dereferenced.circular;
49+
result.value = dereferenced.value;
50+
}
51+
else {
52+
for (let key of Object.keys(obj)) {
53+
let keyPath = Pointer.join(path, key);
54+
let keyPathFromRoot = Pointer.join(pathFromRoot, key);
55+
let value = obj[key];
56+
let circular = false;
57+
58+
if ($Ref.isAllowed$Ref(value, options)) {
59+
dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, dereferencedCache, $refs, options);
60+
circular = dereferenced.circular;
61+
// Avoid pointless mutations; breaks frozen objects to no profit
62+
if (obj[key] !== dereferenced.value) {
63+
obj[key] = dereferenced.value;
64+
}
65+
}
66+
else {
67+
if (parents.indexOf(value) === -1) {
68+
dereferenced = crawl(value, keyPath, keyPathFromRoot, parents, dereferencedCache, $refs, options);
6369
circular = dereferenced.circular;
6470
// Avoid pointless mutations; breaks frozen objects to no profit
6571
if (obj[key] !== dereferenced.value) {
6672
obj[key] = dereferenced.value;
6773
}
6874
}
6975
else {
70-
if (parents.indexOf(value) === -1) {
71-
dereferenced = crawl(value, keyPath, keyPathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
72-
circular = dereferenced.circular;
73-
// Avoid pointless mutations; breaks frozen objects to no profit
74-
if (obj[key] !== dereferenced.value) {
75-
obj[key] = dereferenced.value;
76-
}
77-
}
78-
else {
79-
circular = foundCircularReference(keyPath, $refs, options);
80-
}
76+
circular = foundCircularReference(keyPath, $refs, options);
8177
}
82-
83-
// Set the "isCircular" flag if this or any other property is circular
84-
result.circular = result.circular || circular;
8578
}
86-
}
8779

88-
parents.pop();
80+
// Set the "isCircular" flag if this or any other property is circular
81+
result.circular = result.circular || circular;
82+
}
8983
}
84+
85+
parents.pop();
9086
}
9187

9288
return result;
@@ -104,7 +100,7 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
104100
* @param {$RefParserOptions} options
105101
* @returns {{value: object, circular: boolean}}
106102
*/
107-
function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) {
103+
function dereference$Ref ($ref, path, pathFromRoot, parents, dereferencedCache, $refs, options) {
108104
// console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
109105

110106
let $refPath = url.resolve(path, $ref.$ref);
@@ -129,7 +125,6 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, d
129125
return cache;
130126
}
131127

132-
133128
let pointer = $refs._resolve($refPath, path, options);
134129

135130
if (pointer === null) {
@@ -150,7 +145,7 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, d
150145
// Crawl the dereferenced value (unless it's circular)
151146
if (!circular) {
152147
// Determine if the dereferenced value is circular
153-
let dereferenced = crawl(dereferencedValue, pointer.path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
148+
let dereferenced = crawl(dereferencedValue, pointer.path, pathFromRoot, parents, dereferencedCache, $refs, options);
154149
circular = dereferenced.circular;
155150
dereferencedValue = dereferenced.value;
156151
}

0 commit comments

Comments
 (0)