tc39 / proposal-upsert

ECMAScript Proposal, specs, and reference implementation for Map.prototype.upsert
https://tc39.es/proposal-upsert/
MIT License
185 stars 13 forks source link

Please add reasons why not two new methods insert/update #56

Open leobalter opened 2 weeks ago

leobalter commented 2 weeks ago

My ask is to add a readme piece why this proposal does not consider adding separate methods for update and insert into Map.prototype.

I guess there is already discussions over this topic and I understand there might be a desire to avoid observable points that could affect performance, although the current design smells a bit on getting object methods to call before adding a value:

If HasProperty(handler, "update") is true, then
  Let updateFn be ? Get(handler, "update").
  Let updated be ? Call(updateFn, handler, « e.[[Value]], key, M »).

It also currently throws a conditional TypeError because update is not callable if the key exists in the map, otherwise a TypeError if the key doesn't exist and insert is not present. It's equivalent to running a .get before.

I'm unaware why this can't propose other methods analogue to .add such as insert and update.

ljharb commented 2 weeks ago

The most common use case in my experience is upsert - ie, both.

dminor commented 1 week ago

The most common use case in my experience is upsert - ie, both.

Do you have examples of this? To me it seems like the most common use case is "insert if missing" by providing a default.

It looks like the last time this was discussed in committee, there was a strong desire to split this into two separate methods: https://github.com/tc39/proposal-upsert/issues/31, so I've been assuming any new design work here would involve separating the "insert if missing" from the "update if present" use cases.

ljharb commented 1 week ago

Imagine an object representing a database row as a value in a Map; upsert would be “insert if missing, else update the modified_at timestamp and any updated properties”