weak_map
4.1.3, released 3 days ago by glasberg.dev
A map whose keys are weakly held, and cache functions for memoization.
What it does
Provides a WeakMap where keys are held weakly to attach data to objects without preventing garbage collection. Also includes cache functions for memoizing expensive calculations over immutable state.
AI-written summary of version 4.1.2, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need to attach metadata or cached results to objects you don't control without causing memory leaks
- You want to memoize expensive computations over immutable state that should be discarded when state changes or is no longer used
Skip it when
- You need to iterate over entries or know the length of the map
- You are using mutable state for caching computations
Strengths
- Prevents memory leaks by not keeping keys alive
- Built-in memoization and cache functions for immutable states
- Pure Dart with zero dependencies
Weaknesses
- No iteration, length, keys, or values methods on WeakMap by design
- Requires immutable state objects for proper cache invalidation
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 0
not documented
var map = WeakMap<User, Avatar>();
map[user] = avatar;
var a = map[user];Alternatives
Packages that do the same job and could replace weak_map.
- fast_immutable_collections
Fast immutable lists, sets, maps, and multimaps for Dart
91Compare - sortedmap
A map of objects which can be sorted and filtered on both their key and value.
82Compare - ordered_set
Ordered Set that keeps elements sorted and allows duplicates by priority.
82Compare - collection
Utility functions and classes that extend Dart's collection types and algorithms.
50Compare
Works with
Packages commonly used together with weak_map: ones it builds on, ones built on it, and its own family.