2

Should I care about big-endian machines while using Uint32Array and other TypedArrays that represent more than 1 byte array?

Are there devices with big-endian architecture that can correctly open modern sites that use ArrayBuffer API? How much?

All Android devices are little-endian. I assume that AMD and Intel based computers too.


For example, for little-endian:

const buffer = new TextEncoder().encode("abcd").buffer; // It's [97, 98, 99, 100] bytes
new Uint32Array(buffer); // [1684234849]

new Uint32Array(new Uint8Array([0, 0, 0, 1]).buffer); // [16777216]
new Uint32Array(new Uint8Array([1, 0, 0, 0]).buffer); // [1]

But I assume that on the big-endian machine I will get the other results: [1633837924], [1] and [16777216] correspondingly.


Also I can note that bitwise operations works transparancy with TypeArrays, as if I just work with Number (big-endian).

new Uint32Array(new Uint8Array([0, 0, 0, 1]).buffer)[0] >> 16; // 256
16777216 >> 16; // 256
KeyKi
  • 2,693
  • 3
  • 12
  • 24

1 Answers1

3

If you're concerned about endianness, you can use the DataView interface. This allows you to assert the endianness of a byte array, rather than relying on the platform byte order.

This may be a concern if you expect your code to run on a PowerPC platform like an XBox or PS3.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • 1
    DataView is unfortunately slower than TypedArray views, for performance sensitive operations writing two versions of a same function might be preferred. – Kaiido Aug 21 '20 at 08:33