Set stores duplicates due to reference equality
Quick summary
You added two objects with the same content to a Set — they are different references, so both are kept.
Why this happens
Adding object literals with the same shape to a Set
Expecting Set to deduplicate by deep equality
Reading objects from different sources and adding to a Set
Minimal example
const s = new Set();
s.add({ x: 1 });
s.add({ x: 1 });
console.log(s.size); // 2 — different referencesconst s = new Set();
s.add(JSON.stringify({ x: 1 }));
s.add(JSON.stringify({ x: 1 }));
console.log(s.size); // 1 — same primitive stringTwo object literals { x: 1 } are different references, so the Set keeps both. Serializing to a string key makes them equal primitives and deduplication works.
How to diagnose
Log set.size and compare with the expected unique count
Check whether the values are objects (reference equality) vs primitives
Print the entries to see structurally identical duplicates
How to fix
Use a primitive key (e.g., JSON.stringify or an id) for deduplication
Implement a custom dedupe with a Map keyed by a serialized form
Reuse the same object reference instead of creating new literals
How to prevent
Use primitive keys in Sets for deduplication
Document that Set uses reference equality for objects