Closed Bug 885531 Opened 11 years ago Closed 10 years ago

For Map and Set, use SameValueZero as comparator

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 952870

People

(Reporter: bbenvie, Assigned: sankha)

References

(Blocks 1 open bug)

Details

A recent change to the ES6 spec changes the signatures of the Map and Set constructors to look like `Map(iterable = undefined, comparator = undefined)`. The comparator, if not undefined, must be the string "is" or a TypeError is thrown. This acts as a toggle for the algorithm used to determine equality, with the default comparison using `SameValueZero`, and "is" switching it to `SameValue`. `SameValueZero` only differs from `SameValue` in that it considers -0 and +0 as equal (it's like `===` but `SameValueZero(NaN, NaN)` is true).

This observably changes the default behavior of Sets and Maps, because prior to this change `SameValue` was the default (and only) comparator.

As currently implemented in SpiderMonkey:
> set.add(-0)
> set.has(0) // false

After implementing this change:
> set.add(-0)
> set.has(0) // true

To achieve the current behavior after this change:
> var set = new Set(undefined, "is")
> set.add(-0)
> set.has(0) // false


See the following ES6 spec sections (May 2013 draft):
* SameValueZero: 9.2.4
* The Map constructor: 15.14.1.1
* The Set constructor: 15.16.1.1
* Map.prototype.[get|set|has|delete] check `[[MapComparator]]`
* Set.prototype.[add|has|delete] check `[[SetComparator]]`

See also: https://github.com/rwldrn/tc39-notes/blob/master/es6/2013-01/jan-31.md#mapset-comparator


Note: this doesn't affect WeakMaps since they can't have 0/-0 as a key.
I am trying to write a patch for this one.
Assignee: general → sankha93
seems, in IE11 and in Chrome
map.get(+0) is not the same as map.get(-0),
may be it is better to change ES6 spec?
The most recent update to the spec removed the constuctor comparator argument, but it still uses SameValueZero as the comparator. I'll inquire on es-discuss about this.
Summary: For Map and Set, implement comparator argument and change to SameValueZero as default → For Map and Set, use SameValueZero as comparator
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.