Basics

What is a Set in JavaScript — and why it finds things thousands of times faster than an array

Illustration: a rack of cubbies — each holds exactly one stone, and a second identical one bounces off

You ask an AI: "mark which products the user has already seen." It writes seen.includes(id) — and it works. While there are a hundred products. Once there are fifty thousand, the page starts to stutter on every click.

Then the AI changes one line: new Set(seen) and seen.has(id). The lag is gone. Let's see what this thing is — and why it answers "have we had this already?" so fast.

What a Set is

A Set is a collection where every value lives exactly once. Put the same thing in twice — you still have one.

const tags = new Set()
tags.add("js")
tags.add("css")
tags.add("js")      // already there — nothing happens
tags.size           // 2

Picture a rack of cubbies. Each cubby is marked with its value. Try to put a second identical stone in — the cubby is taken, the stone bounces off.

A Set has three main moves:

  • add(x) — put it in;
  • has(x) — check whether it's there;
  • delete(x) — take it out.

There's no numbered order like in an array. You can't ask "give me the third item." You can only ask "is it here or not." And that's the one thing a Set does better than anything else.

Why has() is so fast

Ask an array "is 42 in here?" and it answers honestly, but slowly. It walks from the first element to the last and compares each one. Fifty thousand elements — up to fifty thousand comparisons.

A Set works differently. It lays values out in advance so that the value itself tells it which cubby to look in. So has takes roughly one step — whether there are a hundred items or a million.

The difference really shows when the check sits inside a loop. Matching two lists of ten thousand with includes is up to a hundred million comparisons. With a Set — about ten thousand. That's not "a bit faster," that's "freezes" versus "instant."

// slow: for every order, scan the whole list
orders.filter(o => blocked.includes(o.userId))

// fast: build the Set once, then just ask
const blockedSet = new Set(blocked)
orders.filter(o => blockedSet.has(o.userId))

Where it quietly fails

Two traps people trip on most often.

1. It doesn't treat objects as equal.

const s = new Set([{ id: 1 }, { id: 1 }])
s.size   // 2, not 1

To a Set these are two different objects — two separate "boxes," even with the same contents inside. It doesn't compare what's inside; it checks whether it's the very same object. So put simple values into a Set: ids, emails, strings, numbers.

2. A number and a string are different values.

new Set([1, "1"]).size   // 2

An id from the URL arrives as a string, one from the database as a number. You added 1, you check has("1") — you get false. Convert to one type before you add.

Bonus trap: a Set doesn't survive JSON. JSON.stringify(new Set([1, 2])) returns {} — emptiness, with no error at all. Before saving, turn it into an array: [...set].

Set operations — a new superpower

Since 2024, Sets in every modern browser have methods you used to write by hand:

const a = new Set(["js", "css", "html"])
const b = new Set(["css", "react"])

a.intersection(b)   // shared: css
a.difference(b)     // in a, not in b: js, html
a.union(b)          // everything, no duplicates

"Which skills are in both the candidate's profile and the job post," "who subscribed but hasn't bought yet" — now that's one line instead of a ten-line nested loop. If the AI writes you a bulky filter inside a filter for a task like this, ask: "rewrite it with a Set and intersection."

Where you'll run into it

Most often in three places in AI-written code:

  • removing duplicates: [...new Set(emails)] — the shortest way to get a list without repeats;
  • "already processed": a Set of message ids so the same email never goes out twice;
  • selected items: checked boxes in a list — selected.has(id).

The rule is simple. Need order and access by position — array. Need a fast "is it there" and no repeats — Set.

How is a Set different from an array?

An array keeps items in order and allows repeats. A Set allows no repeats and has no access by position, but checks membership almost instantly.

How do I turn a Set back into an array?

Spread it into square brackets: [...mySet] or Array.from(mySet). The order is the one you added the items in.

Does Python have a Set?

Yes, and it works the same way: set(), membership via x in s. Set operations have always been there — a & b for the overlap and a - b for the difference.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →