# How costly is allocating new BufferAttribute array?

**URL:** <https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837>\
**Category:** Questions\
**Tags:** buffergeometry\
**Created:** [May 15, 2018, 5:19pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837 "2018-05-15T17:19:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shayy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/shayy/32/1144_2.png) [@shayy](https://discourse.threejs.org/u/shayy)\
**Post date:** [May 15, 2018, 5:19pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/1 "2018-05-15T17:19:48Z")

</div>

From the docs:

> With regards to updating BufferGeometries, the most important thing to understand is that you cannot resize buffers (this is very costly, basically the equivalent to creating a new geometry). You can however update the content of buffers

Let’s say I pre-allocate an array with size of 1000 (using instancing):

```
geometry.addAttribute( 'myAttribute', new InstancedBufferAttribute(new Float32Array(1000), 3).setDynamic(true));

```

1. What do i do if I need more than 1000? I don’t know prehand how much is the maximum i’ll need.
2. Performance wise - is it better to pre-allocate let’s say 10,000 and re-allocate (how?) later, thus potentially wasting memory but if that’s a good guess than never re-allocate again?

All the examples use a fixed maximum but there is nothing saying how to resize it (even with the cost).

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [May 15, 2018, 6:00pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/2 "2018-05-15T18:00:46Z")

</div>

Resizing is definitely not possible since typed arrays are fixed size. If the array is completely filled, you have to create a new instance (which is expensive). I don’t have performance measurement data to illustrate the respective overhead but it’s a best practice to avoid buffer creation.

> I don’t know prehand how much is the maximum i’ll need.

Sometimes you can delete old entries in a typed array. Or you use it in a circular manner. If it is full, you start at the beginning and overwrite existing values. Can’t you do something like that in your scenario?

---

<div class="post-metadata">

**Author:** ![pailhead](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pailhead/32/85_2.png) [@pailhead](https://discourse.threejs.org/u/pailhead)\
**Post date:** [May 15, 2018, 6:48pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/3 "2018-05-15T18:48:29Z")

</div>

Or make a system that has a tradeoff between various performance.

1. Are you going to create mostly up to 1000, but sometimes 1001?

> create a bigger cache

2. Are you sometimes creating up to 1000, other times up to 5000?

> either 1 or every time you go over a 1000 increment, make a new geometry

3. Is this taking too long?

> try a chunk with 500, 200, 100…

This really depends on your use case, and the best answer is, you’ll have to try.

---

<div class="post-metadata">

**Author:** ![christjt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/christjt/32/17332_2.png) [@christjt](https://discourse.threejs.org/u/christjt)\
**Post date:** [March 26, 2021, 1:18pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/4 "2021-03-26T13:18:09Z")

</div>

If I have to reallocate the BufferAttribute, do I have to make a completly new geometry, or is there some way to just overwrite the BufferAttribute?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 26, 2021, 5:14pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/5 "2021-03-26T17:14:04Z")

</div>

If the size of the buffer changes then yes, you have to call `dispose()` on the geometry and create a new one.

---

<div class="post-metadata">

**Author:** ![trusktr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/trusktr/32/13726_2.png) [@trusktr](https://discourse.threejs.org/u/trusktr)\
**Post date:** [November 7, 2022, 10:19pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/6 "2022-11-07T22:19:51Z")

</div>

Would it be better if individual BufferAttributes were disposable? A BufferGeometry may have multiple BufferAttributes, and there are cases where we only want to grow one of the BufferAttributes.

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [November 7, 2022, 10:36pm UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/7 "2022-11-07T22:36:50Z")

</div>

IMHO yes, it’d be nice if we could dispose BufferAttributes individually. Probably comes down to cost complexity of implementing it in the renderer, whether or not a pull request for that would be accepted.

I’m not sure whether resizing them individually makes sense or not though, probably they should all be the same length at any given time?

---

<div class="post-metadata">

**Author:** ![trusktr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/trusktr/32/13726_2.png) [@trusktr](https://discourse.threejs.org/u/trusktr)\
**Post date:** [November 8, 2022, 1:22am UTC](https://discourse.threejs.org/t/how-costly-is-allocating-new-bufferattribute-array/2837/8 "2022-11-08T01:22:47Z")

</div>

oops, oh, I was thinking uniforms, f.e. accessing data from A based on index of attribute B modulo A length.
