new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Runtime · all subjects

binary-data/typedarray

5 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

TypedArray overview

Typed arrays are a family of classes that provide an Array-like interface for interacting with data in an ArrayBuffer. Whereas a DataView lets you write numbers of varying size at a particular offset, a TypedArray interprets the underlying bytes as an array of numbers, each of a fixed size. TypedArray is an internal JavaScript class; you cannot directly create instances of it, and TypedArray is not defined in the global scope.

TypedArray classes and their interpretations

Uint8Array: every one (1) byte is interpreted as an unsigned 8-bit integer, range 0 to 255. Uint16Array: every two (2) bytes are interpreted as an unsigned 16-bit integer, range 0 to 65535. Uint32Array: every four (4) bytes are interpreted as an unsigned 32-bit integer, range 0 to 4294967295. Int8Array: every one (1) byte is interpreted as a signed 8-bit integer, range -128 to 127. Int16Array: every two (2) bytes are interpreted as a signed 16-bit integer, range -32768 to 32767. Int32Array: every four (4) bytes are interpreted as a signed 32-bit integer, range -2147483648 to 2147483647. Float16Array: every two (2) bytes are interpreted as a 16-bit floating point number, range -6.104e5 to 6.55e4. Float32Array: every four (4) bytes are interpreted as a 32-bit floating point number, range -3.4e38 to 3.4e38. Float64Array: every eight (8) bytes are interpreted as a 64-bit floating point number, range -1.7e308 to 1.7e308. BigInt64Array: every eight (8) bytes are interpreted as a signed BigInt, range -9223372036854775808 to 9223372036854775807. BigUint64Array: every eight (8) bytes are interpreted as an unsigned BigInt, range 0 to 18446744073709551615. Uint8ClampedArray: same as Uint8Array, but automatically clamps to the range 0-255 when assigning a value to an element.

TypedArray creation from ArrayBuffer

To create a typed array from a pre-defined ArrayBuffer: const buf = new ArrayBuffer(10); const arr = new Uint8Array(buf); arr[0] = 30; arr[1] = 60; console.log(arr); // => Uint8Array(10) [ 30, 60, 0, 0, 0, 0, 0, 0, 0, 0 ]. A Uint32 value requires four bytes (32 bits). If the ArrayBuffer is 10 bytes long, creating a Uint32Array throws a RangeError because there is no way to cleanly divide 10 bytes into 4-byte chunks. To fix this, create a typed array over a particular slice of the ArrayBuffer by providing a byteOffset and length: const arr = new Uint32Array(buf, 0, 2);

TypedArray creation without ArrayBuffer

You do not need to create an ArrayBuffer instance first. Pass a length to the typed array constructor instead: const arr = new Uint8Array(5); // all elements are initialized to zero => Uint8Array(5) [0, 0, 0, 0, 0]. Typed arrays can also be instantiated directly from an array of numbers: const arr1 = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);. Or from another typed array: const arr2 = new Uint8Array(arr);

TypedArray methods

Typed arrays provide the same methods as regular arrays, with a few exceptions. Methods like push and pop are not available because they would require resizing the underlying ArrayBuffer. Supported methods include: filter(), map(), reduce(), forEach(), every(), find(), includes(), and indexOf().

Give your agent this brain