Default value argument for Map/WeakMap.prototype.get()

Andrea Giammarchi andrea.giammarchi at
Wed Nov 28 14:07:48 PST 2012

Good, so now my only concern is about the fact ternary or || is better for
GC and performance, i.e.

var query = map.get('queried', $('myquery'));

If you repeat above example code 100 times, for 100 times you'll re-query
the DOM.
I might be the only one here but I can see already abuses of this second

This is as example not true using the ternary or the || operator

var query = map.has('queried') ? map.get('queried') : $('myquery');

// or quick and dirty
var query = map.get('queried') || $('myquery');

but in both cases $('myquery') is performed many times because there's no
shortcut able to set and return values, which is eventually the next logic
thing to change to this API.

What I believe is equivalently needed is a set method that returns the set
value so that

var query = map.has('queried') ? map.get('queried') :
map.set('queried', $('myquery'));

// or quick and dirty
var query = map.get('queried') || map.set('queried', $('myquery'));

in both cases the $('myquery') will be invoked only once, accordingly with
the fact it's returning an object and not a falsy value.

This is also much more familiar for developers that are used to write such

var query = obj.queried || (obj.queried = $('myquery'));

This is in my opinion the best practice while, going back to the beginning
of this reply, the extra argument to the get() method will bring to
developers a bad pattern, IMHO

  1. it's easy to forget to store it later
  2. it executes eventually the expensive assignment/procedure regardless
the key is present or not

At least point 1 could be solved with a third argument, false by default,
able to store automatically the defaultValue if missing.

map.get('queried', $('myquery'), true).each(whatever);

Above example will ensure that map.get('queried') next time will return the
very first defaultValue that has been stored ...

Here, a universe of ambiguity created by a second argument that more I
write about it, less makes sense even if handy.

My 2 cents

On Wed, Nov 28, 2012 at 1:46 PM, Allen Wirfs-Brock <allen at>wrote:

> It isn't in the wiki proposal, so it probably didn't get included when I
> created those  chapter 15 sections.  If there is consensus on those
> optional arguments I can add them.
> Allen
> On Nov 28, 2012, at 1:15 PM, Brendan Eich wrote:
> > Agreed.
> >
> > Allen, did this default optional parameter to get() get dropped by
> accident?
> >
> > /be
> >
> > Tab Atkins Jr. wrote:
> >> On Wed, Nov 28, 2012 at 12:12 PM, Andrea Giammarchi
> >> <andrea.giammarchi at>  wrote:
> >>> apologies, I meant it is trivial, but it depends what you are looking
> for /
> >>> need
> >>>
> >>> So, since is that easy to create your own case, then no reason to add
> the
> >>> default argument in the API
> >>
> >> No, that's not the case.  Even if it's "trivial" to write it on your
> >> own, if its use-case is sufficiently common, adding it helps with the
> >> overall usability of the language.  After all, by your argument, we
> >> don't need Map#has either, since you can get around it by just doing:
> >>
> >> function mapset(map, key, val) { map.set(key, [val]); }
> >> function mapget(map, key) { return map.get(key)[0]; }
> >> function maphas(map, key) { return map.get(key) === undefined; }
> >>
> >> This lets you store undefined in a map, and still distinguish it from
> >> the key not being set at all, while using solely Map#get and Map#set,
> >> and a trivial bit of code.
> >>
> >> In this case, I agree that getting a default value from a map when the
> >> key is undefined is extremely common (I do it regularly when using
> >> dictionaries in Python), and it should be part of the default API.
> >>
> >> ~TJ
> >> _______________________________________________
> >> es-discuss mailing list
> >> es-discuss at
> >>
> >>
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list