v143 · javascript

Upsert

ECMAScript proposal for Map.prototype.getOrInsert, Map.prototype.getOrInsertComputed, WeakMap.prototype.getOrInsert, and WeakMap.prototype.getOrInsertComputed.

concepts

  1. Map.getOrInsert(Computed)

    Live: call getOrInsert and getOrInsertComputed on a real Map and see which calls insert vs hit. Probes the WeakMap variants too. Polyfills when missing.

  2. Memoize cache

    The canonical TC39 motivation: collapse "has + get + compute + set" into one call. Side-by-side hit/miss counters on a memoized Fibonacci.

  3. 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.

  4. Map getOrInsert Benchmark

    Native Map.prototype.getOrInsert and getOrInsertComputed detection 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.

  5. WeakMap Counter

    DOM elements as WeakMap keys: WeakMap.prototype.getOrInsert tracks per-node click counts without manual cleanup. When a node is removed, the entry is eligible for garbage collection automatically — unlike a plain Map. 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.

references