Suggestions for Set

Allen Wirfs-Brock allen at wirfs-brock.com
Wed Oct 3 09:03:25 PDT 2012


A couple observations:

1) this whole issue is only relevant  if you actually have array objects containing holes.  Special treat for sparse arrays doesn't hurt anybody (other than implementors)  who doesn't use them.

2)  If we (and the broader developer community) don't like sparse behavior for Array then rather than introducing additional inconsistencies about the handling of hole (for example, making Array.forEach and for-of on a sparse array different)  it would be better to simply see if we can eliminate (or compartmentalize) the sparse behavior of arrays.

Some possible approaches

1)  Make all array instances dense and see what really breaks.  Possibly provide a new SparseArray "subclass". Anyone brave enough to do the experiment?

2)  Formalize the concept of sparseness into either a selectable attribute of arrays or as a special "subclass" of array.  Most current obviously non-sparse usages get the dense version:

var aDenseArray = [0,1,2,3,4,5,6,7];
var aSparseArray = [,,,,,,7];

console.log.(aDenseArray.isSparse());  //false
console.log.(aSparseArray.isSparse());  //true

var dense2 = new Array(0,1,2,3,4,5);

var dense3 = new Array(1000);
console.log.(dense3.isSparse());  //false

var sparse2 = new Array(1001);
console.log.(dense3.isSparse());  //true

var sparse3 = Array.sparse();
console.log.(sparse3.isSparse());  //true

Allen


More information about the es-discuss mailing list