Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does a JavaScript `Set` determine whether two values are duplicates?

Quick answer

A JavaScript Set keeps values unique using SameValueZero equality. Re-adding a primitive value already in the set does not create another entry. Objects are compared by identity, not by their properties: adding the same object reference twice gives one entry, but two separately created objects with matching properties give two. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m adding values to a JavaScript Set. What does it count as a duplicate?

Geez

A Set allows each value only once according to SameValueZero equality. For primitive values, adding the same value again does not add a second entry. Objects work differently from a comparison of their contents: a Set checks whether the object is the same reference. So two objects that look alike can both be entries. [S1]

That distinction matters when a value is simple, like a number, versus when it is an object created separately elsewhere in your code.

Follow-up question

Can you show the primitive and object cases together, including the sizes I should get?

Geez

Here is a small example you can run:

const primitives = new Set(["tea", "tea", 7, 7]);
console.log(primitives.size); // 2

const item = { name: "tea" };
const sameReference = new Set([item, item]);
console.log(sameReference.size); // 1

const lookalikes = new Set([
  { name: "tea" },
  { name: "tea" }
]);
console.log(lookalikes.size); // 2

The primitive set has one entry for "tea" and one for 7. In the second case, both entries refer to the same object. In the last case, each object literal creates a separate object, so matching name properties do not make them duplicates. [S1]

Follow-up question

Why doesn’t the Set compare the two objects’ name properties for me?

Geez

Because object uniqueness in a Set is based on identity: whether the references point to the same object, rather than whether their properties match. In the example, item is one object reference used twice. The two object literals are separate references, even though both have a name property with the same value. [S1]

A useful way to check what your code is doing is to look at where each object is created. Reusing a variable reuses its reference; creating another object literal creates another object. If your goal is to treat matching properties as duplicates, define that comparison explicitly rather than expecting the Set to compare object contents.

Follow-up question

What should I check if I expected two records with the same ID to collapse into one entry?

Geez

Check whether the records are separate objects and whether you are adding the records themselves or their IDs. Separate record objects remain distinct Set entries even when their id properties match. If uniqueness should be based on IDs, one direct option is to build a set from those primitive IDs:

const records = [
  { id: "a1", label: "First" },
  { id: "a1", label: "Second" }
];

const ids = new Set(records.map(record => record.id));
console.log(ids.size); // 1

This example deduplicates the ID values; it does not select or combine a single record for you. Decide separately which record to keep if you need a deduplicated record list. The Set rule does not determine your application’s preferred handling of records that share an ID but differ elsewhere.

A practical checklist

  • Decide whether uniqueness should mean the same primitive value, the same object reference, or a matching property such as an ID.

  • For primitives, check whether the same value is being added more than once.

  • For objects, check whether you are reusing one reference or creating separate objects.

  • If matching a property defines a duplicate, compare or collect that property explicitly.

  • When deduplicating records by a property, decide how to handle records that share that property but differ elsewhere.

A few more useful answers

Will two object literals with identical properties count as one Set entry?

No. Separately created objects have separate identities, so both can be stored even when their properties match. [S1]

What happens if I add the same object variable twice?

The variable refers to the same object both times, so the Set contains one entry for that object. [S1]

Can I use a Set to get unique record IDs?

Yes. Add the ID values to a Set rather than adding the record objects. That gives unique IDs, but does not decide which record to keep.

Sources

References used for this guide.

  1. [S1] Set — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work