# StaticArray: Array on the Stack

**URL:** <https://es.discourse.group/t/staticarray-array-on-the-stack/1340>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [May 9, 2022, 5:28pm UTC](https://es.discourse.group/t/staticarray-array-on-the-stack/1340 "2022-05-09T17:28:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nagayev](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nagayev](https://es.discourse.group/u/nagayev)\
**Post date:** [May 9, 2022, 5:28pm UTC](https://es.discourse.group/t/staticarray-array-on-the-stack/1340/1 "2022-05-09T17:28:52Z")

</div>

github repo: [GitHub - nagayev/static-array-proposal: Ecmascript proposal](https://github.com/nagayev/static-array-proposal)

# Static array proposal

## Champion

Looking for a champion.

## Status

Stage 0, discussing the idea.

## Motivation

In system languages like C or D user can create arrays with constant size like

```c
int main(){
  int a[25];
  return 0;
}

```

Such arrays creates on the stack, not on the heap.  
These means allocation takes less time.

So I suggest add to ECMAScript object `StaticArray` with Array like API excluding possibility to change Array's size.

## Polyfills

As `StaticArray` is just Array, but located at the stack we user can use usual Array.

---

<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:** [May 9, 2022, 6:07pm UTC](https://es.discourse.group/t/staticarray-array-on-the-stack/1340/2 "2022-05-09T18:07:53Z")

</div>

Hi @nagayev

For many JS engines allocating an array is more complex than knowing its length. They also optimise the object based on the values that are inserted (e.g int/floats).

If an object is short lived it is also likely to be allocated in the ‘nursery’ which is a fast bump allocator. So there may not be a big gain in using the stack here.

---

<div class="post-metadata">

**Author:** ![nagayev](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nagayev](https://es.discourse.group/u/nagayev)\
**Post date:** [May 9, 2022, 8:33pm UTC](https://es.discourse.group/t/staticarray-array-on-the-stack/1340/3 "2022-05-09T20:33:29Z")

</div>

@aclaymore Thank you for your response.

> Blockquote  
> If an object is short lived it is also likely to be allocated in the ‘nursery’ which is a fast bump allocator.

It's an interesting fact, I haven't known it. But what about objects with long life?  
Maybe this optimization is still relevant for such cases.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [May 9, 2022, 9:32pm UTC](https://es.discourse.group/t/staticarray-array-on-the-stack/1340/4 "2022-05-09T21:32:12Z")

</div>

If it's long-lived, you couldn't allocate it on the stack at all (or you'd have a problem).

---

<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:** [May 10, 2022, 5:39pm UTC](https://es.discourse.group/t/staticarray-array-on-the-stack/1340/5 "2022-05-10T17:39:55Z")

</div>

I wouldn't be a fan of stack-based allocations either, because you'll have to deal with problems like this somehow, and there really isn't any pretty solution to this that doesn't go against the intuition of how arrays work.

```javascript
function doThing(callback) {
  const myStackArray = <createStackArraySomehow>
  callback(myStackArray)
}

// elsewhere
let array = [] // Set it to an initial value
doThing(stackArray => array = stackArray)
console.log(array) // What happens?

```
