Nick Krempel (2014-02-22T04:01:40.000Z)
And here's another sort of case where sparse arrays are useful, a more
concrete example:

A partial application function which takes a function (as the 'this'
argument in this example) and a (sparse) array specifying which parameters
to bind:
f.partial(['a', , 'b']) is then different from f.partial(['a', undefined,
'b']) in that the former binds 'a' to the first parameter and 'b' to the
third parameter (leaving the second parameter unbound), while the latter
binds 'a' to the first parameter, undefined to the second parameter, and
'b' to the third parameter.
You could use an object like {0: 'a', 2: 'c'} but this is clearly clunkier
syntax (and likely a problem in performance-critical code too, as
number->string conversions are happening in many engines here). You could
use a Map with only integer keys, but this is also clunky and is more
weakly typed in that non-integer keys potentially have to be checked for.

Nick


On 22 February 2014 03:50, Nick Krempel <ndkrempel at google.com> wrote:

> A sparse array is useful whenever the index represents an externally
> meaningful piece of data (like an ID).
>
> This use case could be replaced with a Map which just uses integer keys,
> but I believe JavaScript engines are better optimized working with Arrays
> here, where the keys are known to be integers. So in some cases when you
> care about performance, you may want to use a sparse array. (For some
> JavaScript engines and some ranges of array size you may in fact be better
> off with a dense array for performance, even if it means spending time
> filling it with undefined. That may not be an option if 'undefined' is a
> meaningful value differing from missing.)
>
> Nick
>
>
>
> On 21 February 2014 21:50, C. Scott Ananian <ecmascript at cscott.net> wrote:
>
>> I actually just responded in more depth over at
>>
>> http://esdiscuss.org/topic/what-does-is-not-present-mean-in-spec-algorithms#content-9
>>
>> Let's continue the discussion over there.
>>  --scott
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140222/520ab1dc/attachment.html>
domenic at domenicdenicola.com (2014-03-02T22:43:23.258Z)
And here's another sort of case where sparse arrays are useful, a more
concrete example:

A partial application function which takes a function (as the 'this'
argument in this example) and a (sparse) array specifying which parameters
to bind: `f.partial(['a', , 'b'])` is then different from `f.partial(['a', undefined, 'b'])` in that the former binds 'a' to the first parameter and 'b' to the
third parameter (leaving the second parameter unbound), while the latter
binds 'a' to the first parameter, undefined to the second parameter, and
'b' to the third parameter.
You could use an object like `{0: 'a', 2: 'c'}` but this is clearly clunkier
syntax (and likely a problem in performance-critical code too, as
number->string conversions are happening in many engines here). You could use a Map with only integer keys, but this is also clunky and is more weakly typed in that non-integer keys potentially have to be checked for.