DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on β€’ Edited on β€’ Originally published at thinkthroo.com

1

WeakSet() in react-scan source code.

In this article, we review a code snippet from react-scan source code.

export const ignoredProps = new WeakSet<
  Exclude<ReactNode, undefined | null | string | number | boolean | bigint>
>();

export const ignoreScan = (node: ReactNode) => {
  if (node && typeof node === 'object') {
    ignoredProps.add(node);
  }
};
Enter fullscreen mode Exit fullscreen mode

I found this code snippet in a file, core/index.ts. Why not use a proper Set? why would react-scan author decide to use WeakSet? To draw some conclusion around that, we first need to understand difference between WeakSet and Set in JavaScript.

Image description

WeakSet

A WeakSet is a collection of garbage-collectable values, including objects and non-registered symbols. A value in the WeakSet may only occur once. It is unique in the WeakSet's collection.

Read more about WeakSet.

Set

The Set object lets you store unique values of any type, whether primitive values or object references.

Read more about Set

WeakSet vs Set

Values of WeakSets must be garbage-collectable. Most primitive data types can be arbitrarily created and don’t have a lifetime, so they cannot be stored. Objects and non-registered symbols can be stored because they are garbage-collectable.

The main differences to the Set object are:

  • WeakSets are collections of objects and symbols only. They cannot contain arbitrary values of any type, as Sets can.

  • The WeakSet is weak, meaning references to objects in a WeakSet are held weakly. If no other references to a value stored in the WeakSet exist, those values can be garbage collected.

Coming back to the code snippet in react-scan

export const ignoreScan = (node: ReactNode) => {
  if (node && typeof node === 'object') {
    ignoredProps.add(node);
  }
};
Enter fullscreen mode Exit fullscreen mode

Now it makes sense why typeof node === β€˜object’ check is in place. Can you guess why? this is because only Object and Symbols can be stored in the WeakMap that are garbage collectable if they are not referenced elsewhere.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com

My Githubβ€Šβ€”β€Š https://github.com/ramu-narasinga

My websiteβ€Šβ€”β€Š https://ramunarasinga.com

My Youtube channelβ€Šβ€”β€Š https://www.youtube.com/@ramu-narasinga

Learning platformβ€Šβ€”β€Š https://thinkthroo.com

Codebase Architectureβ€Šβ€”β€Š https://app.thinkthroo.com/architecture

Best practicesβ€Šβ€”β€Š https://app.thinkthroo.com/best-practices

Production-grade projectsβ€Šβ€”β€Š https://app.thinkthroo.com/production-grade-projects

References:

  1. https://github.com/aidenybai/react-scan/blob/main/packages/scan/src/core/index.ts#L609

  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet

Full-stack web apps in pure Java.

Full-stack web apps in pure Java.

Build full-stack web apps entirely in Java. No JavaScript required. Vaadin is open source, secure and ready for production.

Learn More

Top comments (2)

Collapse
 
fyodorio profile image
Fyodor β€’

That was insightful and practical dude, thanks πŸ‘πŸΌ

Collapse
 
ramunarasinga-11 profile image
Ramu Narasinga β€’

Thanks, exactly why I study OSS. πŸ˜‰

agentic postgres

An MCP Server That Actually Understands Postgres

We took 10+ years of Postgres experience and turned it into built-in prompts. Agents get tools for schema design, query optimization, and migrations, plus they can search Postgres docs on the fly.

Learn more