Suggested answer

WeakMap: A Map where keys must be objects (not primitives). Keys are held weakly — if no other reference to the key object exists, the key-value pair is garbage collected automatically. Not iterable (no .forEach, .keys(), .size). Use case: storing private data associated with DOM elements or class instances without preventing garbage collection: const privateData = new WeakMap(); class MyClass { constructor() { privateData.set(this, { secret: 42 }); } }. WeakSet: A Set of objects held weakly. Not iterable. Use case: tracking which objects have been processed without preventing GC (e.g., marking visited nodes in a graph traversal). Regular Map and Set hold strong references — objects stored as keys/values will not be GC'd even if nothing else references them. WeakMap/WeakSet are useful for memory-sensitive caching.

Practice content for interview preparation; not an official vendor answer. Verify details against current product documentation.

Community comments (0)

No comments yet.

Sign in or create a free account to add a comment. Comments are moderated before they appear.

Plain text only, 3–2000 characters. A moderator reviews every comment before it is published.