Map.prototype.clear method

# Jason Orendorff (12 years ago)

An actual real-world user of ES6 Map (in Firefox-only code) is asking for a .clear() method:

<jcranmer> I'm writing a mork reader in JS for mconley's new

addressbook (using JS workers, OS.File, and JS Maps !) <jcranmer> one of the operations I need to be able to do amounts to "clear the entire table" <jcranmer> I can't just make it a new map, since I put other

properties on the map that I don't want to lose <jcranmer> I'm doing this for now: <jcranmer> for (let [tid, value] of this._currentTable) <jcranmer> this._currentTable.delete(tid); <jorendorff> why store those extra properties on this._currentTable

rather than directly on this? <jcranmer> because I have multiple tables

In our current implementation, a builtin Map.prototype.clear method that simply removes all entries from the Map would be quite a bit faster than what jcranmer is currently doing.

# Brendan Eich (12 years ago)

+1, no brainer. Thanks for supplying the brain!

# Yehuda Katz (12 years ago)

A clear method is definitely important for both Map and Set. Our real-world polyfills have also needed them.

What about copying of these new data structures?

# Kris Kowal (12 years ago)

On Mon, Oct 22, 2012 at 10:58 AM, Yehuda Katz <wycats at gmail.com> wrote:

What about copying of these new data structures?

The polyfills I have also do .clone(depth_opt, memo_opt), where memo is a map that breaks cycles and depth defaults to Infinity, 0 means return this, and 1 is a shallow copy.

kriskowal/collections

Kris Kowal

# Axel Rauschmayer (12 years ago)

What about copying of these new data structures?

It should be possible to initialize a collection via another collection (right?). I prefer copy-constructors to clone() methods.

# Yehuda Katz (12 years ago)

On Mon, Oct 22, 2012 at 11:11 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

What about copying of these new data structures?

It should be possible to initialize a collection via another collection (right?). I prefer copy-constructors to clone() methods.

That would be fine with me, if supported.

# Domenic Denicola (12 years ago)

On Oct 22, 2012, at 14:28, "Yehuda Katz" <wycats at gmail.com<mailto:wycats at gmail.com>> wrote:

On Mon, Oct 22, 2012 at 11:11 AM, Axel Rauschmayer <axel at rauschma.de<mailto:axel at rauschma.de>> wrote:

What about copying of these new data structures?

It should be possible to initialize a collection via another collection (right?). I prefer copy-constructors to clone() methods.

That would be fine with me, if supported.

Isn't it already specced to work, for Set at least, since it accepts any iterable?

let copy = new Set(original);

I don't recall offhand if Map accepts an iterable of [key, val] pairs, but if it doesn't yet, maybe this provides an argument that it should.

# Yehuda Katz (12 years ago)

On Mon, Oct 22, 2012 at 11:32 AM, Domenic Denicola < domenic at domenicdenicola.com> wrote:

On Oct 22, 2012, at 14:28, "Yehuda Katz" <wycats at gmail.com> wrote:

On Mon, Oct 22, 2012 at 11:11 AM, Axel Rauschmayer <axel at rauschma.de>wrote:

What about copying of these new data structures?

It should be possible to initialize a collection via another collection (right?). I prefer copy-constructors to clone() methods.

That would be fine with me, if supported.

Isn't it already specced to work, for Set at least, since it accepts any iterable?

let copy = new Set(original);

I don't recall offhand if Map accepts an iterable of [key, val] pairs, but if it doesn't yet, maybe this provides an argument that it should.

Sounds good. Hopefully VMs can optimize the implementation when a Map/Set is passed in.

# Jason Orendorff (12 years ago)

On Mon, Oct 22, 2012 at 1:32 PM, Domenic Denicola <domenic at domenicdenicola.com> wrote:

On Mon, Oct 22, 2012 at 11:11 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

What about copying of these new data structures?

Isn't it already specced to work, for Set at least, since it accepts any iterable?

let copy = new Set(original);

I don't recall offhand if Map accepts an iterable of [key, val] pairs, but if it doesn't yet, maybe this provides an argument that it should.

Yes, it is specified that way in the latest draft. The Map in Firefox supports this.