Fredrik Anfinsen
Fredrik Anfinsen

Reputation: 97

JavaScript is not allocating needed size for ArrayBuffer

I tried to run this code to allocate a ArrayBuffer:

var data = new ArrayBuffer(336000000);
console.log(data.byteLength); // outputs 125829120

Does anyone know why the number if bytes is not allocated? Chrome don't give me any errors / warnings.

This problem is not on every platform, but on several like on Galaxy S6 (Android Browser)

Upvotes: 3

Views: 220

Answers (1)

CupawnTae
CupawnTae

Reputation: 14580

You're trying to allocate more memory than is available. However, according to the spec, this should raise an exception rather than returning you a smaller buffer:

http://www.ecma-international.org/ecma-262/6.0/#sec-createbytedatablock

Let db be a new Data Block value consisting of size bytes. If it is impossible to create such a Data Block, throw a RangeError exception.

And the previous spec https://www.khronos.org/registry/typedarray/specs/latest/#5

If the requested number of bytes could not be allocated an exception is raised

So if your browser is returning a smaller buffer rather than throwing an exception, it's non-compliant.

Upvotes: 4

Related Questions