Will Javascript create space for unasssigned array indices? -
Will Javascript create space for unasssigned array indices? -
i have code similar this:
var somearray = new array(); somearray[0] = "stuff"; somearray[1] = "things"; somearray[10] = "more stuff"; somearray[20] = "other things"; somearray[100] = "far away stuff";
and on.
if this, there memory allocated indices 1-9, 11-19, , 21-99? amount important plenty worry if array added info @ 10,000, or other high number?
i know can like
somearray["abc"] = "foo";
and treats property on object named somearray, used like
somearray.abc = "foo";
so case same?
edit: apparently reply bit terse/fuzzy some.
as stated, memory allocation behavior in illustration unspecified. is, the ecmascript specification doesn't tell engines how much memory (not) allocate unassigned array slots.
what really happens under hood, then, varies lot engine. example, given engine may take pre-allocate 100bytes of memory new arrays assumption packed, , subsequently de-opt sparse array implementation if ratio of assigned values array size isn't met. that's discretion of engine implementors though, since, again, language specification silent here.
that beingness said, array sparsity rather critical concept language (despite memory allocation thereof beingness unspecified). example, specifications several of built-in array.prototype
methods (such .map()
, .foreach()
) explicitly define iteration behavior on "holes" in arrays (that is, indices have never been assigned to). additionally, real-world code takes advantage of array sparsity well. these factors set lot of pressure level on engine implementors not have engine fall on , die when encountering code like:
var arr = []; arr[4294967295] = 'allocate this';
in fact, go ahead , seek it. you'll notice engine worthy of note (v8, nitro, spidermonkey, nashorn, rhino, chakra, etc.) handle fine.
now, why works in each of these engine it's own discussion, summarily speaking, it's because expect, , can adequately cope with, sparse arrays.
javascript arrays
Comments
Post a Comment