Skip to content

Commit 02e6fa2

Browse files
committed
Use Set instead of array
Sets should offer faster checking to see if a property has been seen
1 parent 8a6b38e commit 02e6fa2

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/vs/base/common/objects.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,18 @@ export function equals(one: any, other: any): boolean {
179179
}
180180

181181
/**
182-
* Calls JSON.Stringify with a replacer to break apart any circular references.
183-
* This prevents JSON.stringify from throwing the exception
182+
* Calls `JSON.Stringify` with a replacer to break apart any circular references.
183+
* This prevents `JSON`.stringify` from throwing the exception
184184
* "Uncaught TypeError: Converting circular structure to JSON"
185185
*/
186186
export function safeStringify(obj: any): string {
187-
const seen: any[] = [];
187+
const seen = new Set<any>();
188188
return JSON.stringify(obj, (key, value) => {
189189
if (isObject(value) || Array.isArray(value)) {
190-
if (seen.indexOf(value) !== -1) {
190+
if (seen.has(value)) {
191191
return '[Circular]';
192192
} else {
193-
seen.push(value);
193+
seen.add(value);
194194
}
195195
}
196196
return value;

0 commit comments

Comments
 (0)