-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf - use explicit isCircular code to avoid slow JSON.stringify sett…
…ings
- Loading branch information
Showing
2 changed files
with
32 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* is-circular.ts | ||
* | ||
* Copyright (C) 2025 Posit Software, PBC | ||
*/ | ||
|
||
// deno-lint-ignore no-explicit-any | ||
export const isCircular = (obj: any): unknown => { | ||
const objectSet = new WeakSet(); | ||
// deno-lint-ignore no-explicit-any | ||
const detect = (obj: any): boolean => { | ||
if (obj && typeof obj === "object") { | ||
if (objectSet.has(obj)) { | ||
return true; | ||
} | ||
objectSet.add(obj); | ||
for (const key in obj) { | ||
if (Object.hasOwn(obj, key) && detect(obj[key])) { | ||
return true; | ||
} | ||
} | ||
objectSet.delete(obj); | ||
} | ||
return false; | ||
}; | ||
return detect(obj); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters