# Weak/Map and Weak/Set \`put(key, value): value\` anyone?

**URL:** <https://es.discourse.group/t/weak-map-and-weak-set-put-key-value-value-anyone/2633>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [July 28, 2026, 7:45pm UTC](https://es.discourse.group/t/weak-map-and-weak-set-put-key-value-value-anyone/2633 "2026-07-28T19:45:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WebReflection](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/webreflection/32/248_2.png) [@WebReflection](https://es.discourse.group/u/WebReflection)\
**Post date:** [July 28, 2026, 7:45pm UTC](https://es.discourse.group/t/weak-map-and-weak-set-put-key-value-value-anyone/2633/1 "2026-07-28T19:45:33Z")

</div>

I've written a utility library that provides extends of Map, Set, WeakMap and WeakSet that do only one thing: add a `put(key, value):value` method because returning `this` has been a long-time-ago battle of mine, suggesting chaining was the wrong returning type for those primitives, but I wonder if there's still hope for such simple addiction to ECMAScript.

### Background

`getOrInsert` and `getOrInsertComputed` are great but not enough and not CPU/RAM friendly:

- `getOrInsert` assumes the value has been computed anyway
- `getOrInsertComputed` requires allocation for a callback when inlined

Meet `put(...)` !!!

### Put

Specifications are deadly simple:

- it adds an entry and returns its value instead of the reference itself

```javascript
const obj = map.get(ref) ?? map.put(ref, stuff(ref));
const other = set.has(ref) ? ref : set.put(stuff(ref));

```

For Weak/Map like operations it never requires a runtime function yet it does branch around possible JIT optimizations while for Weak/Set like operations it allows to work on an item before storing it internally.

The current counter-part would require `bind` or other operations that cannot be ignored while executing:

```javascript
const obj = map.getOrInsertComputed(ref, () => stuff(ref));
// useless CPU/RAM/GC operations ^^^^^^^^^^^^^^^^
const other = set.has(ref) ? ref : (set.add(stuff(ref)), ref);
// not really DX friendly ^^^^^^^^^^^^^^^^^^^^^^^^^^

```

Both are way uglier when we consider that `??` operator does a wonderful job with Weak/Map and nobody ever wanted the `set` reference back inline ... **thoughts**?

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [July 28, 2026, 9:14pm UTC](https://es.discourse.group/t/weak-map-and-weak-set-put-key-value-value-anyone/2633/2 "2026-07-28T21:14:11Z")

</div>

I was thinking it would be called `insert`:

> <https://github.com/tc39/proposal-upsert/issues/60#issuecomment-2631967514>
>
> The proposal would add two new methods to Map:
> \- \`getOrInsert(key, default)\` - R…eturn value corresponding to the key if present, otherwise insert the specified default value and return that.
> \- \`getOrInsertComputed(key, callback)\` - Return value corresponding to the key if present, otherwise call the callback function to determine the default value to insert and return.
> 
> Those names are placeholders, I'm opening this issue so we can bikeshed the final names to be used.

So `getOrInsert` would literally mean you are either calling `get` or `insert` depending if the key is already present

---

<div class="post-metadata">

**Author:** ![WebReflection](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/webreflection/32/248_2.png) [@WebReflection](https://es.discourse.group/u/WebReflection)\
**Post date:** [July 28, 2026, 9:49pm UTC](https://es.discourse.group/t/weak-map-and-weak-set-put-key-value-value-anyone/2633/3 "2026-07-28T21:49:25Z")

</div>

I don't have any strong opinion around the name ( **edit** : as long as it's not 20 chars long ), it's rather the utility I am after ... it should just insert and return whatever was inserted, as simple as that!
