24 Keyed Collections
24.1 Map Objects
Maps are collections of key/value pairs where both the keys and values may be arbitrary
Maps must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Maps. It is not intended to be a viable implementation model.
24.1.1 The Map Constructor
The Map
- is
%Map% . - is the initial value of the
"Map" property of theglobal object . - creates and initializes a new Map when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value in an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specified Map behaviour must include asupercall to the Mapconstructor to create and initialize the subclass instance with the internal state necessary to support theMap.prototypebuilt-in methods.
24.1.1.1 Map ( [ iterable ] )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - Let map be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[MapData]] »).%Map.prototype% " - Set map.[[MapData]] to a new empty
List . - If iterable is either
undefined ornull , return map. - Let adder be ?
Get (map,"set" ). - If
IsCallable (adder) isfalse , throw aTypeError exception. - Return ?
AddEntriesFromIterable (map, iterable, adder).
If the parameter iterable is present, it is expected to be an object that implements a
24.1.1.2 AddEntriesFromIterable ( target, iterable, adder )
The abstract operation AddEntriesFromIterable takes arguments target (an Object), iterable (an
- Let iteratorRecord be ?
GetIterator (iterable,sync ). - Repeat,
- Let next be ?
IteratorStepValue (iteratorRecord). - If next is
done , return target. - If next
is not an Object , then- Let error be
ThrowCompletion (a newly createdTypeError object). - Return ?
IteratorClose (iteratorRecord, error).
- Let error be
- Let k be
Completion (Get (next,"0" )). IfAbruptCloseIterator (k, iteratorRecord).- Let v be
Completion (Get (next,"1" )). IfAbruptCloseIterator (v, iteratorRecord).- Let status be
Completion (Call (adder, target, « k, v »)). IfAbruptCloseIterator (status, iteratorRecord).
- Let next be ?
The parameter iterable is expected to be an object that implements a
24.1.2 Properties of the Map Constructor
The Map
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
24.1.2.1 Map.groupBy ( items, callback )
callback should be a function that accepts two arguments. groupBy calls callback once for each element in items, in ascending order, and constructs a new Map. Each value returned by callback is used as a key in the Map. For each such key, the result Map has an entry whose key is that key and whose value is an array containing all the elements for which callback returned that key.
callback is called with two arguments: the value of the element and the index of the element.
The return value of groupBy is a Map.
This function performs the following steps when called:
- Let groups be ?
GroupBy (items, callback,collection ). - Let map be !
Construct (%Map% ). - For each
Record { [[Key]], [[Elements]] } g of groups, do- Let elements be
CreateArrayFromList (g.[[Elements]]). - Let entry be the
Record { [[Key]]: g.[[Key]], [[Value]]: elements }. - Append entry to map.[[MapData]].
- Let elements be
- Return map.
24.1.2.2 Map.prototype
The initial value of Map.prototype is the
This property has the attributes { [[Writable]]:
24.1.2.3 get Map [ %Symbol.species% ]
Map[%Symbol.species%] is an
- Return the
this value.
The value of the
Methods that create derived collection objects should call
24.1.3 Properties of the Map Prototype Object
The Map prototype object:
- is
%Map.prototype% . - has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[MapData]] internal slot.
24.1.3.1 Map.prototype.clear ( )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- Set p.[[Key]] to
empty . - Set p.[[Value]] to
empty .
- Set p.[[Key]] to
- Return
undefined .
The existing [[MapData]]
24.1.3.2 Map.prototype.constructor
The initial value of Map.prototype.constructor is
24.1.3.3 Map.prototype.delete ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - Set key to
CanonicalizeKeyedCollectionKey (key). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Key]] to
empty . - Set p.[[Value]] to
empty . - Return
true .
- Set p.[[Key]] to
- If p.[[Key]] is not
- Return
false .
The value
24.1.3.4 Map.prototype.entries ( )
This method performs the following steps when called:
- Let M be the
this value. - Return ?
CreateMapIterator (M,key+value ).
24.1.3.5 Map.prototype.forEach ( callback [ , thisArg ] )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - If
IsCallable (callback) isfalse , throw aTypeError exception. - Let entries be M.[[MapData]].
- Let numEntries be the number of elements in entries.
- Let index be 0.
- Repeat, while index < numEntries,
- Let e be entries[index].
- Set index to index + 1.
- If e.[[Key]] is not
empty , then- Perform ?
Call (callback, thisArg, « e.[[Value]], e.[[Key]], M »). - NOTE: The number of elements in entries may have increased during execution of callback.
- Set numEntries to the number of elements in entries.
- Perform ?
- Return
undefined .
callback should be a function that accepts three arguments. forEach calls callback once for each key/value pair present in the Map, in key insertion order. callback is called only for keys of the Map which actually exist; it is not called for keys that have been deleted from the Map.
If a thisArg parameter is provided, it will be used as the
callback is called with three arguments: the value of the item, the key of the item, and the Map being traversed.
forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callback. Each entry of a map's [[MapData]] is only visited once. New keys added after the call to forEach begins are visited. A key will be revisited if it is deleted after it has been visited and then re-added before the forEach call completes. Keys that are deleted after the call to forEach begins and before being visited are not visited unless the key is added again before the forEach call completes.
24.1.3.6 Map.prototype.get ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - Set key to
CanonicalizeKeyedCollectionKey (key). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Return
undefined .
24.1.3.7 Map.prototype.getOrInsert ( key, value )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - Set key to
CanonicalizeKeyedCollectionKey (key). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[MapData]].
- Return value.
24.1.3.8 Map.prototype.getOrInsertComputed ( key, callback )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - If
IsCallable (callback) isfalse , throw aTypeError exception. - Set key to
CanonicalizeKeyedCollectionKey (key). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Let value be ?
Call (callback,undefined , « key »). - NOTE: The Map may have been modified during execution of callback.
- For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Value]] to value.
- Return value.
- If p.[[Key]] is not
- Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[MapData]].
- Return value.
24.1.3.9 Map.prototype.has ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - Set key to
CanonicalizeKeyedCollectionKey (key). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , returntrue .
- If p.[[Key]] is not
- Return
false .
24.1.3.10 Map.prototype.keys ( )
This method performs the following steps when called:
- Let M be the
this value. - Return ?
CreateMapIterator (M,key ).
24.1.3.11 Map.prototype.set ( key, value )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - Set key to
CanonicalizeKeyedCollectionKey (key). - For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Value]] to value.
- Return M.
- If p.[[Key]] is not
- Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[MapData]].
- Return M.
24.1.3.12 get Map.prototype.size
Map.prototype.size is an
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[MapData]]). - Let count be 0.
- For each
Record { [[Key]], [[Value]] } p of M.[[MapData]], do- If p.[[Key]] is not
empty , set count to count + 1.
- If p.[[Key]] is not
- Return
𝔽 (count).
24.1.3.13 Map.prototype.values ( )
This method performs the following steps when called:
- Let M be the
this value. - Return ?
CreateMapIterator (M,value ).
24.1.3.14 Map.prototype [ %Symbol.iterator% ] ( )
The initial value of the
24.1.3.15 Map.prototype [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
24.1.4 Properties of Map Instances
Map instances are
24.1.5 Map Iterator Objects
A Map Iterator is an object that represents a specific iteration over some specific Map instance object. There is not a named
24.1.5.1 CreateMapIterator ( map, kind )
The abstract operation CreateMapIterator takes arguments map (an
- Perform ?
RequireInternalSlot (map, [[MapData]]). - Let closure be a new
Abstract Closure with no parameters that captures map and kind and performs the following steps when called:- Let entries be map.[[MapData]].
- Let index be 0.
- Let numEntries be the number of elements in entries.
- Repeat, while index < numEntries,
- Let e be entries[index].
- Set index to index + 1.
- If e.[[Key]] is not
empty , then- If kind is
key , then- Let result be e.[[Key]].
- Else if kind is
value , then- Let result be e.[[Value]].
- Else,
Assert : kind iskey+value .- Let result be
CreateArrayFromList (« e.[[Key]], e.[[Value]] »).
- Perform ?
GeneratorYield (CreateIteratorResultObject (result,false )). - NOTE: The number of elements in entries may have increased while execution of this abstract operation was paused by
GeneratorYield . - Set numEntries to the number of elements in entries.
- If kind is
- Return
NormalCompletion (unused ).
- Return
CreateIteratorFromClosure (closure," ,%MapIteratorPrototype% "%MapIteratorPrototype% ).
24.1.5.2 The %MapIteratorPrototype% Object
The
- has properties that are inherited by all
Map Iterator objects . - is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%Iterator.prototype% . - has the following properties:
24.1.5.2.1 %MapIteratorPrototype% .next ( )
- Return ?
.GeneratorResume (this value,empty ," )%MapIteratorPrototype% "
24.1.5.2.2 %MapIteratorPrototype% [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
24.2 Set Objects
Set objects are collections of
Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Set objects. It is not intended to be a viable implementation model.
24.2.1 Abstract Operations For Set Objects
24.2.1.1 Set Records
A Set Record is a
Set Records have the fields listed in
| Field Name | Value | Meaning |
|---|---|---|
| [[SetObject]] | an Object | the Set or similar object. |
| [[Size]] |
a non-negative |
The reported size of the object. |
| [[Has]] |
a |
The has method of the object.
|
| [[Keys]] |
a |
The keys method of the object.
|
24.2.1.2 GetSetRecord ( obj )
The abstract operation GetSetRecord takes argument obj (an
- If obj
is not an Object , throw aTypeError exception. - Let rawSize be ?
Get (obj,"size" ). - Let numSize be ?
ToNumber (rawSize). - NOTE: If rawSize is
undefined , then numSize will beNaN . - If numSize is
NaN , throw aTypeError exception. - Let intSize be !
ToIntegerOrInfinity (numSize). - If intSize < 0, throw a
RangeError exception. - Let has be ?
Get (obj,"has" ). - If
IsCallable (has) isfalse , throw aTypeError exception. - Let keys be ?
Get (obj,"keys" ). - If
IsCallable (keys) isfalse , throw aTypeError exception. - Return a new
Set Record { [[SetObject]]: obj, [[Size]]: intSize, [[Has]]: has, [[Keys]]: keys }.
24.2.1.3 SetDataHas ( setData, value )
The abstract operation SetDataHas takes arguments setData (a
- If
SetDataIndex (setData, value) isnot-found , returnfalse . - Return
true .
24.2.1.4 SetDataIndex ( setData, value )
The abstract operation SetDataIndex takes arguments setData (a
- Set value to
CanonicalizeKeyedCollectionKey (value). - Let size be the number of elements in setData.
- Let index be 0.
- Repeat, while index < size,
- Let e be setData[index].
- If e is not
empty and e is value, then- Return index.
- Set index to index + 1.
- Return
not-found .
24.2.1.5 SetDataSize ( setData )
The abstract operation SetDataSize takes argument setData (a
- Let count be 0.
- For each element e of setData, do
- If e is not
empty , set count to count + 1.
- If e is not
- Return count.
24.2.2 The Set Constructor
The Set
- is
%Set% . - is the initial value of the
"Set" property of theglobal object . - creates and initializes a new
Set object when called as aconstructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value in an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specified Set behaviour must include asupercall to the Setconstructor to create and initialize the subclass instance with the internal state necessary to support theSet.prototypebuilt-in methods.
24.2.2.1 Set ( [ iterable ] )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - Let set be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[SetData]] »).%Set.prototype% " - Set set.[[SetData]] to a new empty
List . - If iterable is either
undefined ornull , return set. - Let adder be ?
Get (set,"add" ). - If
IsCallable (adder) isfalse , throw aTypeError exception. - Let iteratorRecord be ?
GetIterator (iterable,sync ). - Repeat,
- Let next be ?
IteratorStepValue (iteratorRecord). - If next is
done , return set. - Let status be
Completion (Call (adder, set, « next »)). IfAbruptCloseIterator (status, iteratorRecord).
- Let next be ?
24.2.3 Properties of the Set Constructor
The Set
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
24.2.3.1 Set.prototype
The initial value of Set.prototype is the
This property has the attributes { [[Writable]]:
24.2.3.2 get Set [ %Symbol.species% ]
Set[%Symbol.species%] is an
- Return the
this value.
The value of the
Methods that create derived collection objects should call
24.2.4 Properties of the Set Prototype Object
The Set prototype object:
- is
%Set.prototype% . - has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[SetData]] internal slot.
24.2.4.1 Set.prototype.add ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[SetData]]). - Set value to
CanonicalizeKeyedCollectionKey (value). - For each element e of S.[[SetData]], do
- If e is not
empty andSameValue (e, value) istrue , then- Return S.
- If e is not
- Append value to S.[[SetData]].
- Return S.
24.2.4.2 Set.prototype.clear ( )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[SetData]]). - For each element e of S.[[SetData]], do
- Replace the element of S.[[SetData]] whose value is e with an element whose value is
empty .
- Replace the element of S.[[SetData]] whose value is e with an element whose value is
- Return
undefined .
The existing [[SetData]]
24.2.4.3 Set.prototype.constructor
The initial value of Set.prototype.constructor is
24.2.4.4 Set.prototype.delete ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[SetData]]). - Set value to
CanonicalizeKeyedCollectionKey (value). - For each element e of S.[[SetData]], do
- If e is not
empty andSameValue (e, value) istrue , then- Replace the element of S.[[SetData]] whose value is e with an element whose value is
empty . - Return
true .
- Replace the element of S.[[SetData]] whose value is e with an element whose value is
- If e is not
- Return
false .
The value
24.2.4.5 Set.prototype.difference ( other )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[SetData]]). - Let otherRec be ?
GetSetRecord (other). - Let resultSetData be a copy of O.[[SetData]].
- If
SetDataSize (O.[[SetData]]) ≤ otherRec.[[Size]], then- Let thisSize be the number of elements in O.[[SetData]].
- Let index be 0.
- Repeat, while index < thisSize,
- Else,
- Let keysIter be ?
GetIteratorFromMethod (otherRec.[[SetObject]], otherRec.[[Keys]]). - Let next be
not-started . - Repeat, while next is not
done ,- Set next to ?
IteratorStepValue (keysIter). - If next is not
done , then- Set next to
CanonicalizeKeyedCollectionKey (next). - Let valueIndex be
SetDataIndex (resultSetData, next). - If valueIndex is not
not-found , then- Set resultSetData[valueIndex] to
empty .
- Set resultSetData[valueIndex] to
- Set next to
- Set next to ?
- Let keysIter be ?
- Let result be
OrdinaryObjectCreate (%Set.prototype% , « [[SetData]] »). - Set result.[[SetData]] to resultSetData.
- Return result.
24.2.4.6 Set.prototype.entries ( )
This method performs the following steps when called:
- Let S be the
this value. - Return ?
CreateSetIterator (S,key+value ).
For iteration purposes, a Set appears similar to a Map where each entry has the same value for its key and value.
24.2.4.7 Set.prototype.forEach ( callback [ , thisArg ] )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[SetData]]). - If
IsCallable (callback) isfalse , throw aTypeError exception. - Let entries be S.[[SetData]].
- Let numEntries be the number of elements in entries.
- Let index be 0.
- Repeat, while index < numEntries,
- Let e be entries[index].
- Set index to index + 1.
- If e is not
empty , then- Perform ?
Call (callback, thisArg, « e, e, S »). - NOTE: The number of elements in entries may have increased during execution of callback.
- Set numEntries to the number of elements in entries.
- Perform ?
- Return
undefined .
callback should be a function that accepts three arguments. forEach calls callback once for each value present in the
If a thisArg parameter is provided, it will be used as the
callback is called with three arguments: the first two arguments are a value contained in the Set. The same value is passed for both arguments. The
The callback is called with three arguments to be consistent with the call back functions used by forEach methods for Map and Array. For Sets, each item value is considered to be both the key and the value.
forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callback.
Each value is normally visited only once. However, a value will be revisited if it is deleted after it has been visited and then re-added before the forEach call completes. Values that are deleted after the call to forEach begins and before being visited are not visited unless the value is added again before the forEach call completes. New values added after the call to forEach begins are visited.
24.2.4.8 Set.prototype.has ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[SetData]]). - Set value to
CanonicalizeKeyedCollectionKey (value). - For each element e of S.[[SetData]], do
- If e is not
empty andSameValue (e, value) istrue , returntrue .
- If e is not
- Return
false .
24.2.4.9 Set.prototype.intersection ( other )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[SetData]]). - Let otherRec be ?
GetSetRecord (other). - Let resultSetData be a new empty
List . - If
SetDataSize (O.[[SetData]]) ≤ otherRec.[[Size]], then- Let thisSize be the number of elements in O.[[SetData]].
- Let index be 0.
- Repeat, while index < thisSize,
- Let e be O.[[SetData]][index].
- Set index to index + 1.
- If e is not
empty , then- Let inOther be
ToBoolean (?Call (otherRec.[[Has]], otherRec.[[SetObject]], « e »)). - If inOther is
true , then- NOTE: It is possible for earlier calls to otherRec.[[Has]] to remove and re-add an element of O.[[SetData]], which can cause the same element to be visited twice during this iteration.
- If
SetDataHas (resultSetData, e) isfalse , then- Append e to resultSetData.
- NOTE: The number of elements in O.[[SetData]] may have increased during execution of otherRec.[[Has]].
- Set thisSize to the number of elements in O.[[SetData]].
- Let inOther be
- Else,
- Let keysIter be ?
GetIteratorFromMethod (otherRec.[[SetObject]], otherRec.[[Keys]]). - Let next be
not-started . - Repeat, while next is not
done ,- Set next to ?
IteratorStepValue (keysIter). - If next is not
done , then- Set next to
CanonicalizeKeyedCollectionKey (next). - Let inThis be
SetDataHas (O.[[SetData]], next). - If inThis is
true , then- NOTE: Because other is an arbitrary object, it is possible for its
"keys" iterator to produce the same value more than once. - If
SetDataHas (resultSetData, next) isfalse , then- Append next to resultSetData.
- NOTE: Because other is an arbitrary object, it is possible for its
- Set next to
- Set next to ?
- Let keysIter be ?
- Let result be
OrdinaryObjectCreate (%Set.prototype% , « [[SetData]] »). - Set result.[[SetData]] to resultSetData.
- Return result.
24.2.4.10 Set.prototype.isDisjointFrom ( other )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[SetData]]). - Let otherRec be ?
GetSetRecord (other). - If
SetDataSize (O.[[SetData]]) ≤ otherRec.[[Size]], then- Let thisSize be the number of elements in O.[[SetData]].
- Let index be 0.
- Repeat, while index < thisSize,
- Let e be O.[[SetData]][index].
- Set index to index + 1.
- If e is not
empty , then
- Else,
- Let keysIter be ?
GetIteratorFromMethod (otherRec.[[SetObject]], otherRec.[[Keys]]). - Let next be
not-started . - Repeat, while next is not
done ,- Set next to ?
IteratorStepValue (keysIter). - If next is not
done , then- If
SetDataHas (O.[[SetData]], next) istrue , then- Perform ?
IteratorClose (keysIter,NormalCompletion (unused )). - Return
false .
- Perform ?
- If
- Set next to ?
- Let keysIter be ?
- Return
true .
24.2.4.11 Set.prototype.isSubsetOf ( other )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[SetData]]). - Let otherRec be ?
GetSetRecord (other). - If
SetDataSize (O.[[SetData]]) > otherRec.[[Size]], returnfalse . - Let thisSize be the number of elements in O.[[SetData]].
- Let index be 0.
- Repeat, while index < thisSize,
- Let e be O.[[SetData]][index].
- Set index to index + 1.
- If e is not
empty , then
- Return
true .
24.2.4.12 Set.prototype.isSupersetOf ( other )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[SetData]]). - Let otherRec be ?
GetSetRecord (other). - If
SetDataSize (O.[[SetData]]) < otherRec.[[Size]], returnfalse . - Let keysIter be ?
GetIteratorFromMethod (otherRec.[[SetObject]], otherRec.[[Keys]]). - Let next be
not-started . - Repeat, while next is not
done ,- Set next to ?
IteratorStepValue (keysIter). - If next is not
done , then- If
SetDataHas (O.[[SetData]], next) isfalse , then- Perform ?
IteratorClose (keysIter,NormalCompletion (unused )). - Return
false .
- Perform ?
- If
- Set next to ?
- Return
true .
24.2.4.13 Set.prototype.keys ( )
The initial value of the
For iteration purposes, a Set appears similar to a Map where each entry has the same value for its key and value.
24.2.4.14 get Set.prototype.size
Set.prototype.size is an
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[SetData]]). - Let size be
SetDataSize (S.[[SetData]]). - Return
𝔽 (size).
24.2.4.15 Set.prototype.symmetricDifference ( other )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[SetData]]). - Let otherRec be ?
GetSetRecord (other). - Let keysIter be ?
GetIteratorFromMethod (otherRec.[[SetObject]], otherRec.[[Keys]]). - Let resultSetData be a copy of O.[[SetData]].
- Let next be
not-started . - Repeat, while next is not
done ,- Set next to ?
IteratorStepValue (keysIter). - If next is not
done , then- Set next to
CanonicalizeKeyedCollectionKey (next). - Let resultIndex be
SetDataIndex (resultSetData, next). - If resultIndex is
not-found , let alreadyInResult befalse ; else let alreadyInResult betrue . - If
SetDataHas (O.[[SetData]], next) istrue , then- If alreadyInResult is
true , set resultSetData[resultIndex] toempty .
- If alreadyInResult is
- Else,
- If alreadyInResult is
false , append next to resultSetData.
- If alreadyInResult is
- Set next to
- Set next to ?
- Let result be
OrdinaryObjectCreate (%Set.prototype% , « [[SetData]] »). - Set result.[[SetData]] to resultSetData.
- Return result.
24.2.4.16 Set.prototype.union ( other )
This method performs the following steps when called:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[SetData]]). - Let otherRec be ?
GetSetRecord (other). - Let keysIter be ?
GetIteratorFromMethod (otherRec.[[SetObject]], otherRec.[[Keys]]). - Let resultSetData be a copy of O.[[SetData]].
- Let next be
not-started . - Repeat, while next is not
done ,- Set next to ?
IteratorStepValue (keysIter). - If next is not
done , then- Set next to
CanonicalizeKeyedCollectionKey (next). - If
SetDataHas (resultSetData, next) isfalse , then- Append next to resultSetData.
- Set next to
- Set next to ?
- Let result be
OrdinaryObjectCreate (%Set.prototype% , « [[SetData]] »). - Set result.[[SetData]] to resultSetData.
- Return result.
24.2.4.17 Set.prototype.values ( )
This method performs the following steps when called:
- Let S be the
this value. - Return ?
CreateSetIterator (S,value ).
24.2.4.18 Set.prototype [ %Symbol.iterator% ] ( )
The initial value of the
24.2.4.19 Set.prototype [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
24.2.5 Properties of Set Instances
Set instances are
24.2.6 Set Iterator Objects
A Set Iterator is an
24.2.6.1 CreateSetIterator ( set, kind )
The abstract operation CreateSetIterator takes arguments set (an
- Perform ?
RequireInternalSlot (set, [[SetData]]). - Let closure be a new
Abstract Closure with no parameters that captures set and kind and performs the following steps when called:- Let index be 0.
- Let entries be set.[[SetData]].
- Let numEntries be the number of elements in entries.
- Repeat, while index < numEntries,
- Let e be entries[index].
- Set index to index + 1.
- If e is not
empty , then- If kind is
key+value , then- Let result be
CreateArrayFromList (« e, e »). - Perform ?
GeneratorYield (CreateIteratorResultObject (result,false )).
- Let result be
- Else,
Assert : kind isvalue .- Perform ?
GeneratorYield (CreateIteratorResultObject (e,false )).
- NOTE: The number of elements in entries may have increased while execution of this abstract operation was paused by
GeneratorYield . - Set numEntries to the number of elements in entries.
- If kind is
- Return
NormalCompletion (unused ).
- Return
CreateIteratorFromClosure (closure," ,%SetIteratorPrototype% "%SetIteratorPrototype% ).
24.2.6.2 The %SetIteratorPrototype% Object
The
- has properties that are inherited by all
Set Iterator objects . - is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%Iterator.prototype% . - has the following properties:
24.2.6.2.1 %SetIteratorPrototype% .next ( )
- Return ?
.GeneratorResume (this value,empty ," )%SetIteratorPrototype% "
24.2.6.2.2 %SetIteratorPrototype% [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
24.3 WeakMap Objects
WeakMaps are collections of key/value pairs where the keys are objects and/or symbols and values may be arbitrary
An implementation may impose an arbitrarily determined latency between the time a key/value pair of a WeakMap becomes inaccessible and the time when the key/value pair is removed from the WeakMap. If this latency was observable to ECMAScript program, it would be a source of indeterminacy that could impact program execution. For that reason, an ECMAScript implementation must not provide any means to observe a key of a WeakMap that does not require the observer to present the observed key.
WeakMaps must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of key/value pairs in the collection. The data structure used in this specification is only intended to describe the required observable semantics of WeakMaps. It is not intended to be a viable implementation model.
WeakMap and WeakSet are intended to provide mechanisms for dynamically associating state with an object or symbol in a manner that does not “leak” memory resources if, in the absence of the WeakMap or WeakSet instance, the object or symbol otherwise became inaccessible and subject to resource reclamation by the implementation's garbage collection mechanisms. This characteristic can be achieved by using an inverted per-object/symbol mapping of WeakMap or WeakSet instances to keys. Alternatively, each WeakMap or WeakSet instance may internally store its key and value data, but this approach requires coordination between the WeakMap or WeakSet implementation and the garbage collector. The following references describe mechanism that may be useful to implementations of WeakMap and WeakSet:
Barry Hayes. 1997. Ephemerons: a new finalization mechanism. In Proceedings of the 12th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA '97), A. Michael Berman (Ed.). ACM, New York, NY, USA, 176-183, http://doi.acm.org/10.1145/263698.263733.
Alexandra Barros, Roberto Ierusalimschy, Eliminating Cycles in Weak Tables. Journal of Universal Computer Science - J.UCS, vol. 14, no. 21, pp. 3481-3497, 2008, http://www.jucs.org/jucs_14_21/eliminating_cycles_in_weak
24.3.1 The WeakMap Constructor
The WeakMap
- is
%WeakMap% . - is the initial value of the
"WeakMap" property of theglobal object . - creates and initializes a new WeakMap when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value in an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specified WeakMap behaviour must include asupercall to the WeakMapconstructor to create and initialize the subclass instance with the internal state necessary to support theWeakMap.prototypebuilt-in methods.
24.3.1.1 WeakMap ( [ iterable ] )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - Let map be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[WeakMapData]] »).%WeakMap.prototype% " - Set map.[[WeakMapData]] to a new empty
List . - If iterable is either
undefined ornull , return map. - Let adder be ?
Get (map,"set" ). - If
IsCallable (adder) isfalse , throw aTypeError exception. - Return ?
AddEntriesFromIterable (map, iterable, adder).
If the parameter iterable is present, it is expected to be an object that implements a
24.3.2 Properties of the WeakMap Constructor
The WeakMap
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
24.3.2.1 WeakMap.prototype
The initial value of WeakMap.prototype is the
This property has the attributes { [[Writable]]:
24.3.3 Properties of the WeakMap Prototype Object
The WeakMap prototype object:
- is
%WeakMap.prototype% . - has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[WeakMapData]] internal slot.
24.3.3.1 WeakMap.prototype.constructor
The initial value of WeakMap.prototype.constructor is
24.3.3.2 WeakMap.prototype.delete ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , returnfalse . - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Key]] to
empty . - Set p.[[Value]] to
empty . - Return
true .
- Set p.[[Key]] to
- If p.[[Key]] is not
- Return
false .
The value
24.3.3.3 WeakMap.prototype.get ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , returnundefined . - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Return
undefined .
24.3.3.4 WeakMap.prototype.getOrInsert ( key, value )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , throw aTypeError exception. - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[WeakMapData]].
- Return value.
24.3.3.5 WeakMap.prototype.getOrInsertComputed ( key, callback )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , throw aTypeError exception. - If
IsCallable (callback) isfalse , throw aTypeError exception. - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Let value be ?
Call (callback,undefined , « key »). - NOTE: The WeakMap may have been modified during execution of callback.
- For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Value]] to value.
- Return value.
- If p.[[Key]] is not
- Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[WeakMapData]].
- Return value.
24.3.3.6 WeakMap.prototype.has ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , returnfalse . - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , returntrue .
- If p.[[Key]] is not
- Return
false .
24.3.3.7 WeakMap.prototype.set ( key, value )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , throw aTypeError exception. - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Value]] to value.
- Return M.
- If p.[[Key]] is not
- Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[WeakMapData]].
- Return M.
24.3.3.8 WeakMap.prototype [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
24.3.4 Properties of WeakMap Instances
WeakMap instances are
24.4 WeakSet Objects
WeakSets are collections of objects and/or symbols. A distinct object or symbol may only occur once as an element of a WeakSet's collection. A WeakSet may be queried to see if it contains a specific value, but no mechanism is provided for enumerating the values it holds. In certain conditions, values which are not
An implementation may impose an arbitrarily determined latency between the time a value contained in a WeakSet becomes inaccessible and the time when the value is removed from the WeakSet. If this latency was observable to ECMAScript program, it would be a source of indeterminacy that could impact program execution. For that reason, an ECMAScript implementation must not provide any means to determine if a WeakSet contains a particular value that does not require the observer to present the observed value.
WeakSets must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of WeakSets. It is not intended to be a viable implementation model.
See the NOTE in
24.4.1 The WeakSet Constructor
The WeakSet
- is
%WeakSet% . - is the initial value of the
"WeakSet" property of theglobal object . - creates and initializes a new WeakSet when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value in an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specified WeakSet behaviour must include asupercall to the WeakSetconstructor to create and initialize the subclass instance with the internal state necessary to support theWeakSet.prototypebuilt-in methods.
24.4.1.1 WeakSet ( [ iterable ] )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - Let set be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[WeakSetData]] »).%WeakSet.prototype% " - Set set.[[WeakSetData]] to a new empty
List . - If iterable is either
undefined ornull , return set. - Let adder be ?
Get (set,"add" ). - If
IsCallable (adder) isfalse , throw aTypeError exception. - Let iteratorRecord be ?
GetIterator (iterable,sync ). - Repeat,
- Let next be ?
IteratorStepValue (iteratorRecord). - If next is
done , return set. - Let status be
Completion (Call (adder, set, « next »)). IfAbruptCloseIterator (status, iteratorRecord).
- Let next be ?
24.4.2 Properties of the WeakSet Constructor
The WeakSet
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
24.4.2.1 WeakSet.prototype
The initial value of WeakSet.prototype is the
This property has the attributes { [[Writable]]:
24.4.3 Properties of the WeakSet Prototype Object
The WeakSet prototype object:
- is
%WeakSet.prototype% . - has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[WeakSetData]] internal slot.
24.4.3.1 WeakSet.prototype.add ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
CanBeHeldWeakly (value) isfalse , throw aTypeError exception. - For each element e of S.[[WeakSetData]], do
- If e is not
empty andSameValue (e, value) istrue , then- Return S.
- If e is not
- Append value to S.[[WeakSetData]].
- Return S.
24.4.3.2 WeakSet.prototype.constructor
The initial value of WeakSet.prototype.constructor is
24.4.3.3 WeakSet.prototype.delete ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
CanBeHeldWeakly (value) isfalse , returnfalse . - For each element e of S.[[WeakSetData]], do
- If e is not
empty andSameValue (e, value) istrue , then- Replace the element of S.[[WeakSetData]] whose value is e with an element whose value is
empty . - Return
true .
- Replace the element of S.[[WeakSetData]] whose value is e with an element whose value is
- If e is not
- Return
false .
The value
24.4.3.4 WeakSet.prototype.has ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
CanBeHeldWeakly (value) isfalse , returnfalse . - For each element e of S.[[WeakSetData]], do
- If e is not
empty andSameValue (e, value) istrue , returntrue .
- If e is not
- Return
false .
24.4.3.5 WeakSet.prototype [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
24.4.4 Properties of WeakSet Instances
WeakSet instances are
24.5 Abstract Operations for Keyed Collections
24.5.1 CanonicalizeKeyedCollectionKey ( key )
The abstract operation CanonicalizeKeyedCollectionKey takes argument key (an
- If key is
-0 𝔽, return+0 𝔽. - Return key.