Lodash _.isWeakSet() method
What you’ll learn
- How
_.isWeakSet(value)detects nativeWeakSetinstances. - Why
Set,WeakMap, and plain objects always returnfalse. - How the internal-tag check survives cross-realm boundaries.
- Where WeakSets fit—tagging objects without blocking garbage collection.
Prerequisites
You understand the difference between Set (iterable, strong refs) and WeakSet (object-only, weak refs).
- You have used
new WeakSet()to track objects (visited nodes, processed instances, etc.). - Try-it labs load lodash from the CDN.
Overview
Use _.isWeakSet before reaching for add/has/delete, or whenever you need to confirm a value really is a weak-reference set rather than a regular Set.
Native WeakSet
Matches new WeakSet(); rejects Set, Map, WeakMap.
Cross-realm safe
Tag-based detection covers cases where instanceof would lie.
GC-friendly
Confirms a structure that won’t pin objects in memory.
Syntax
_.isWeakSet(value) - value: any value to test.
- Returns:
truewhenvalueis aWeakSet; otherwisefalse.
new WeakSet instances
The lodash docs baseline: a fresh WeakSet—empty or populated—always qualifies.
import isWeakSet from "lodash/isWeakSet";
const a = {};
const empty = new WeakSet();
const seeded = new WeakSet([a]);
console.log(
"empty: " + isWeakSet(empty) + "\n" + // true (lodash docs)
"seeded: " + isWeakSet(seeded) // true
); Set is not a WeakSet
Lodash docs explicitly contrast: _.isWeakSet(new Set) returns false. WeakMaps fail too—different tag, different shape.
import isWeakSet from "lodash/isWeakSet";
console.log(
"set: " + isWeakSet(new Set()) + "\n" + // false (lodash docs)
"weakMap: " + isWeakSet(new WeakMap()) // false
); Arrays, plain objects, and nullish
Objects that mimic a set API still fail—lodash inspects the internal tag, not duck-typed methods.
import isWeakSet from "lodash/isWeakSet";
const fake = { add() {}, has() {} };
console.log(
"fake: " + isWeakSet(fake) + "\n" + // false
"arr: " + isWeakSet([1, 2, 3]) + "\n" + // false
"nullVal: " + isWeakSet(null) // false
); 📋 _.isWeakSet vs related checks
| API / pattern | Behavior |
|---|---|
_.isWeakSet(x) | true only for native WeakSet; reliable across realms. |
x instanceof WeakSet | Same single-realm result; breaks across iframes/realms. |
_.isSet(x) | Companion check for iterable Set instances. |
_.isWeakMap(x) | Detects WeakMap—different tag. |
Pitfalls to avoid
No .clear() or .size
WeakSet exposes only add, has, and delete. Calling weakSet.clear() throws “is not a function”—don’t copy that pattern from older snippets.
Objects only
Adding a primitive (string, number) throws a TypeError. Wrap primitives in an object first or use a regular Set.
Not iterable
There is no .values(), .forEach(), or for...of. If you need to enumerate, choose Set.
❓ FAQ
Summary
- Purpose: confirm a value is a native
WeakSetbefore calling its tiny API surface. - Remember:
Set,WeakMap, arrays, and POJOs always returnfalse. - Next: browse the rest of Lodash _.lt().
WeakSet has only add, has, and delete—no clear(), no size, no iteration. Code that tries weakSet.clear() throws “is not a function”, which is why _.isWeakSet matters as a guard before you reach for set-like operations.
5 people found this page helpful
