###What is Blitz-Collections?
Blitz-Collections provides ECMAScript 6 (aka ECMAScript Harmony aka ECMAScript
Next) collections to ECMAScript 5 compliant environments. This includes
###Are the WeakMaps really weak?
Mostly. Due to the way Blitz-Collections work and the limitations of
WeakMaps do not keep any object references unless the object is
sealed or the
constructor property is non-configurable. I suspect that objects
meeting one of these two criteria will be rare. Note that these references are
lost when a
clear method is called.
###There are other polyfills that do this. How is this one different?
I have come across two other attempts to provide these collections and, like
the motivation for starting so many projects, I was not completely satisfied.
One attempt entirely relied upon two sets of arrays to simulate
WeakMap and consequently was slow and not even remotely weak. The other
attempt uses a similar approach to what I have taken by adding a non-enumerable
property to objects. What I found unfortunate was that it then tries to conceal
the new property by hijacking methods such as
As far as I have been able to come up with there are only two ways to create a
Map in ECMAScript 5 (or older) environments:
- Use two arrays, one for the keys and the other for the values. This works
just fine for
Maps (though it will be slow, depending on the size extremely slow) but it cannot be used for
- Add properties onto objects which hold their associated values. The issue with this method is it adds "unnatural" properties on objects, it cannot be done on sealed objects (you have to fall back to method one) and an object's associated value could be modified outside the scope of the Map.
For the speed benefit and also to have a mostly functioning
WeakMap I opted
for method two. To alleviate the issues with that method, Blitz-Collections uses
constructor property and does not add values directly to the property but
adds them to the property's
get descriptor. This "hides" any values the object
may be associated with, virtually eliminates accidental manipulation and
almost seems "natural."
This script exposes the three collection constructors under the
API coming soon. In the meantime check out the current working draft for
see the draft API for
Set (it should be the same--if not
please create an issue).