Lodash _.isWeakMap() method
What you’ll learn
- How
_.isWeakMap(value)identifies nativeWeakMapinstances. - Why
Map,WeakSet, and plain objects fail the check. - How tag-based detection stays correct across realms.
- Where weak maps fit—private metadata, DOM-keyed caches, and memoization.
Prerequisites
You know the difference between Map (strong keys) and WeakMap (weak object keys).
- You have used
new WeakMap()for per-instance bookkeeping. - Try-it labs load lodash from the CDN.
Overview
Use _.isWeakMap before invoking weak-map-only operations or when wiring up a cache that must rely on garbage-collection-friendly semantics.
Native WeakMap
Matches new WeakMap(); rejects Map, Set, WeakSet.
Cross-realm safe
Internal tag check covers cases where instanceof fails.
Memory-aware
Gate logic that depends on weak-reference garbage collection.
Syntax
_.isWeakMap(value) - value: any value to test.
- Returns:
truewhenvalueis aWeakMap; otherwisefalse.
new WeakMap instances
The lodash docs baseline: empty or populated WeakMap instances all qualify.
import isWeakMap from "lodash/isWeakMap";
const key = {};
const empty = new WeakMap();
const seeded = new WeakMap([[key, "value"]]);
console.log(
"empty: " + isWeakMap(empty) + "\n" + // true (lodash docs)
"seeded: " + isWeakMap(seeded) // true
); Map is not a WeakMap
Lodash docs explicitly contrast: _.isWeakMap(new Map) returns false.
import isWeakMap from "lodash/isWeakMap";
console.log(
"map: " + isWeakMap(new Map()) + "\n" + // false (lodash docs)
"weakSet: " + isWeakMap(new WeakSet()) // false
); Plain objects, arrays, and nullish
Objects that mimic the WeakMap API still fail—lodash checks the internal tag, not duck-typed shape.
import isWeakMap from "lodash/isWeakMap";
const fake = { get() {}, set() {} };
console.log(
"fake: " + isWeakMap(fake) + "\n" + // false
"arr: " + isWeakMap([]) + "\n" + // false
"nullVal: " + isWeakMap(null) // false
); 📋 _.isWeakMap vs related checks
| API / pattern | Behavior |
|---|---|
_.isWeakMap(x) | true only for native WeakMap; reliable across realms. |
x instanceof WeakMap | Same single-realm result; breaks across iframes/realms. |
_.isMap(x) | Companion check for strong-keyed Map instances. |
_.isWeakSet(x) | Detects WeakSet—different tag. |
Pitfalls to avoid
Primitives forbidden
Adding a string or number key to a WeakMap throws—use object keys (or registered symbols in modern engines).
No iteration / no size
By design, WeakMaps expose get, set, has, delete—but no iterator or count. Don’t reach for them when you need ordered or enumerable data.
Not serializable
JSON.stringify(new WeakMap()) drops the entries—persist any state to a real Map or array first.
❓ FAQ
Summary
- Purpose: verify a value is a native
WeakMapbefore garbage-collection-aware bookkeeping. - Remember:
Map,WeakSet, arrays, and POJOs always returnfalse. - Next: explore more on Lodash _.isWeakSet().
WeakMap keys must be objects (or registered symbols in modern engines); primitive keys throw. Lodash’s _.isWeakMap guard is a quick way to confirm you actually have one before relying on those semantics.
6 people found this page helpful
