v143 · javascript
Upsert
ECMAScript proposal for Map.prototype.getOrInsert, Map.prototype.getOrInsertComputed, WeakMap.prototype.getOrInsert, and WeakMap.prototype.getOrInsertComputed.
concepts
-
Map.getOrInsert(Computed)
Live: call
getOrInsertandgetOrInsertComputedon a real Map and see which calls insert vs hit. Probes the WeakMap variants too. Polyfills when missing. -
Memoize cache
The canonical TC39 motivation: collapse "has + get + compute + set" into one call. Side-by-side hit/miss counters on a memoized Fibonacci.
-
Counter & group-by patterns
Three patterns that get one line shorter and one bug-class smaller with
getOrInsert/getOrInsertComputed: word counter, group-by-key, dedup & tally. Live before/after with editable input. -
Map getOrInsert Benchmark
Native
Map.prototype.getOrInsertandgetOrInsertComputeddetection with a visible pass/fail indicator and polyfills installed if absent. A benchmark runner tests 3 patterns × 5 runs × 50,000 random words, displaying min / median / max timing with a canvas bar chart and a "FASTEST" winner badge. Three runnable use-case presets (word frequency, event counter, nested Map builder) each execute and show live output. -
WeakMap Counter
DOM elements as WeakMap keys:
WeakMap.prototype.getOrInserttracks per-node click counts without manual cleanup. When a node is removed, the entry is eligible for garbage collection automatically — unlike a plainMap. Includes before/after code comparison, live log of insert vs. hit, and GC-friendly remove pattern.
why it shipped
ECMAScript proposal for Map.prototype.getOrInsert, Map.prototype.getOrInsertComputed, WeakMap.prototype.getOrInsert, and WeakMap.prototype.getOrInsertComputed.