Skip to content

Instantly share code, notes, and snippets.

@Amzd
Created January 27, 2026 12:45
Show Gist options
  • Select an option

  • Save Amzd/67d53943ed8d2f25341f7c105c33fbcb to your computer and use it in GitHub Desktop.

Select an option

Save Amzd/67d53943ed8d2f25341f7c105c33fbcb to your computer and use it in GitHub Desktop.
This is the same as Set but supports objects. Like https://github.com/szikszail/object-set-type but fast.
/**
* This is the same as Set but supports objects.
* Like https://github.com/szikszail/object-set-type but fast.
*/
export class ObjectSet<T> {
_set: Set<string>
constructor(arr: T[] = []) {
this._set = new Set(arr.map(x => JSON.stringify(x)))
}
add(value: any): this {
this._set.add(JSON.stringify(value))
return this
}
has(value: any): boolean {
return this._set.has(JSON.stringify(value))
}
*[Symbol.iterator]() {
for (const str of this._set) {
yield JSON.parse(str)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment