Skip to content

Instantly share code, notes, and snippets.

@ardislu
Created March 27, 2024 06:27
Show Gist options
  • Save ardislu/1a7ff72fc38a70a4b4ac40933e67bc91 to your computer and use it in GitHub Desktop.
Save ardislu/1a7ff72fc38a70a4b4ac40933e67bc91 to your computer and use it in GitHub Desktop.
This is the minimum amount of JavaScript (74 characters) to detect the endianness of a platform.
// The magic number 0xFEFF is selected for familiarity with the commonly-used
// byte-order mark (BOM) character (https://en.wikipedia.org/wiki/Byte_order_mark).
const isLE = new Uint8Array(new Uint16Array([0xFEFF]).buffer)[0] === 0xFF;
// Golfed (-12 characters; 62 total characters):
// const isLE=new Uint8Array(new Uint16Array([256]).buffer)[0]<1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment