Array.create and Function.create

Jordan Harband ljharb at gmail.com
Thu Jan 10 08:53:48 UTC 2019


Why would this be better than `const a = []; Object.setPrototypeOf(a,
null)`?

On Thu, Jan 10, 2019 at 12:09 AM Sultan <thysultan at gmail.com> wrote:

> >An array with no prototype wouldn't have any of the iteration methods on
> it...
>
> Yes, that is what is intended with this, similar to an Object.create(null)
> object with number-ed keys.
>
> Alternatively one could look at the objects created from this to be the
> "bare-bones" structure around these data-structures.
>
> That is the in-existence of prototypes and own properties like "length"
> makes it clear that these "flat" objects are intended as author managed
> objects.
>
> There are is no visible default prototype or own properties because the
> author will create, expose and managed these for the data-structure
> explicitly if need be or more commonly choose to not expose the
> data-structure at all and use these for low-level internal book keeping for
> other abstractions.
>
> This would create a new ceiling(or ground-level) for how "low-level" one
> could go with JavaScript if these where part for the language and as a
> secondary consequence allow engines to make stronger assumptions with
> regards to operations on these structs.
>
> On Thu, Jan 10, 2019 at 9:48 AM Jordan Harband <ljharb at gmail.com> wrote:
>
>> An array with no prototype wouldn't have any of the iteration methods on
>> it; a function with no prototype wouldn't have .call/.bind/.apply - length
>> and name are own properties of functions, and length is an own property of
>> an array, so you'd get those regardless.
>>
>> (`Array.from({ length: 1000 })` already creates an array of length 1000
>> without holes, fwiw)
>>
>> On Wed, Jan 9, 2019 at 10:43 PM Sultan <thysultan at gmail.com> wrote:
>>
>>> Identical to Object.create but for Arrays and Functions.
>>>
>>> This method will allow you to create arrays with no prototype.
>>>
>>> This would allow authors the ability to use array objects as state
>>> containers without the need to resort to index-based objects with
>>>
>>> Object.create(null, length)
>>>
>>> When you want to both use an array-like struct as both a property and
>>> index-able map.
>>>
>>> A side-effect of this would afford engines a strong heuristic for
>>> avoiding holey-array look-ups operations when there's no prototype to walk.
>>>
>>> For example the following would create an array with a length of 1000
>>> without "holes".
>>>
>>> const arr = Array.create(null, 1000)
>>>
>>> In addition this could also apply to functions with
>>>
>>> Function.create(null, () => {})
>>>
>>> When you want to use functions as state-containers but don't want any of
>>> the implicit properties(length, name) etc.
>>> _______________________________________________
>>> 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/20190110/8917bf70/attachment.html>


More information about the es-discuss mailing list