# BufferAttribute subclasses force unnecessary memory allocation

**URL:** <https://discourse.threejs.org/t/bufferattribute-subclasses-force-unnecessary-memory-allocation/77567>\
**Category:** Discussion\
**Tags:** bufferattributes\
**Created:** [February 4, 2025, 6:52pm UTC](https://discourse.threejs.org/t/bufferattribute-subclasses-force-unnecessary-memory-allocation/77567 "2025-02-04T18:52:53Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [February 4, 2025, 6:52pm UTC](https://discourse.threejs.org/t/bufferattribute-subclasses-force-unnecessary-memory-allocation/77567/1 "2025-02-04T18:52:53Z")

</div>

For example, check out the `Float32BufferAttribute` class:

> <https://github.com/mrdoob/three.js/blob/d9bc10bd3c4c545da976b3d55ed09d49d94389ad/src/core/BufferAttribute.js#L611-L619>

It is impossible to bypass the forced buffer allocation:

```js
const existingBuffer = new Float32Array(...)

new Float32BufferAttribute(existingBuffer, 3) // forces a new Float32Array, which could be expensive

```

Can we make update these to do a check? For example something like this:

```js
class Float32BufferAttribute extends BufferAttribute {

	constructor( array, itemSize, normalized ) {

		super(

			array instanceof Float32Array ? array : new Float32Array( array ),
			itemSize,
			normalized

		);

	}

}

```

It would be more intuitive, as it would align with the behavior of plain `BufferAttribute` which accepts the array as-is.

This would align more with Three.js patterns in general, where re-allocation is something that is actively avoided, and people typically have the ability to choose tore-allocate or not. This particular case does not offer the choice.

* * *

In my particular case, this led me to not intuit a correct assumption: I thought I could modify the Float32Array directly, but it wasn’t working:

```js
// code that owns the buffer:
const existingBuffer = new Float32Array(...)

// some other component that received the buffer:
someGeometry.attributes.foo = new Float32BufferAttribute(existingBuffer, 3)

// code that owns the buffer:
existingBuffer[123] = 456 // <----------- why not working?

```

---

<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:** [February 4, 2025, 7:11pm UTC](https://discourse.threejs.org/t/bufferattribute-subclasses-force-unnecessary-memory-allocation/77567/2 "2025-02-04T19:11:23Z")

</div>

# Workaround

For now I’ve replaced all instances of `Float32BufferAttribute`, `Uint32BufferAttribute`, etc, with plain `BufferAttribute`, but this makes the data types less obvious across the code. Reading the Float32/Uint32/etc prefixes is useful for semantics when reading various code locations.
