# Fixed object arrays?

**URL:** <https://es.discourse.group/t/fixed-object-arrays/976>\
**Category:** 💡 Ideas\
**Created:** [September 7, 2021, 8:40am UTC](https://es.discourse.group/t/fixed-object-arrays/976 "2021-09-07T08:40:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [September 7, 2021, 8:40am UTC](https://es.discourse.group/t/fixed-object-arrays/976/1 "2021-09-07T08:40:51Z")

</div>

I would like to see a fixed-size object array to complement the fixed-size numeric arrays.

Dynamically-sized arrays are useful for general high-level use, but there are cases where a fixed-size object array would be extremely handy to help augment certain performance optimizations (especially with [circular buffers](https://github.com/nodejs/node/blob/c7222b35899ad352d78f288ddc16c6d19ca79a06/lib/internal/fixed_queue.js)) and better inform the engine of the intent of the code so it can be more properly optimized. Engines already use these or some similar construct internally for a _lot_ as well, too. ([Example](https://github.com/v8/v8/search?q=FixedArray+path%3Asrc))

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [September 7, 2021, 1:34pm UTC](https://es.discourse.group/t/fixed-object-arrays/976/2 "2021-09-07T13:34:27Z")

</div>

This is technically possible today, in a hacky sort of way. Not sure if the browsers bother to optimize this case either. So, it would probably be better to have a dedicated array type for this, but thought I would point it out anyways.

```javascript
const myArray = []
myArray.length = 5
Object.seal(myArray)
myArray[0] = 'a'
console.log(myArray) // ['a', <empty>*4]
myArray.push('b') // Error!

```

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [September 7, 2021, 6:49pm UTC](https://es.discourse.group/t/fixed-object-arrays/976/3 "2021-09-07T18:49:28Z")

</div>

[V8 doesn't even currently optimize the obvious low hanging fruit of `new Array(n).fill(null)`.](https://crbug.com/v8/6892) If I could rely on engines to do event that little bit, I wouldn't be suggesting this in the first place.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [September 7, 2021, 7:24pm UTC](https://es.discourse.group/t/fixed-object-arrays/976/4 "2021-09-07T19:24:52Z")

</div>

If you can't expect that, why would you expect a brand-new construct to be optimized sufficiently for your use cases?

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [September 7, 2021, 7:32pm UTC](https://es.discourse.group/t/fixed-object-arrays/976/5 "2021-09-07T19:32:37Z")

</div>

It can't be resized, and so the idea is it memory-wise would be treated more like a typed array that just happens to also be usable as a GC root. Also, engines already use similar abstractions underneath quite broadly, so I don't see it as a major ask for them to make a few tweaks to expose it and subsequently copy over (with light modifications) the relevant typed array methods. (V8 in particular might not even need to create a new type for it if the `length` property is made an own property for consistency with traditional arrays.)

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [September 7, 2021, 7:55pm UTC](https://es.discourse.group/t/fixed-object-arrays/976/6 "2021-09-07T19:55:37Z")

</div>

Maybe something like this could be achieved with [Fixed-layout-objects](https://github.com/syg/proposal-structs/)  
If one was dynamically created based on the length using eval.  
Admittedly a bit on the hacky side and maybe no faster, or even slower than a regular array.

At the very least it feels somewhat in the same spirit as this other proposal?

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [September 8, 2021, 1:47am UTC](https://es.discourse.group/t/fixed-object-arrays/976/7 "2021-09-08T01:47:42Z")

</div>

Yeah, that could work well enough.
