Skip to content

Commit

Permalink
Use Set instead of array
Browse files Browse the repository at this point in the history
Sets should offer faster checking to see if a property has been seen
  • Loading branch information
mjbvz committed Sep 11, 2020
1 parent 8a6b38e commit 02e6fa2
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/vs/base/common/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,18 @@ export function equals(one: any, other: any): boolean {
}

/**
* Calls JSON.Stringify with a replacer to break apart any circular references.
* This prevents JSON.stringify from throwing the exception
* Calls `JSON.Stringify` with a replacer to break apart any circular references.
* This prevents `JSON`.stringify` from throwing the exception
* "Uncaught TypeError: Converting circular structure to JSON"
*/
export function safeStringify(obj: any): string {
const seen: any[] = [];
const seen = new Set<any>();
return JSON.stringify(obj, (key, value) => {
if (isObject(value) || Array.isArray(value)) {
if (seen.indexOf(value) !== -1) {
if (seen.has(value)) {
return '[Circular]';
} else {
seen.push(value);
seen.add(value);
}
}
return value;
Expand Down

0 comments on commit 02e6fa2

Please sign in to comment.