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/arraybuffer

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

ArrayBuffer basics

An ArrayBuffer is a data structure that represents a sequence of bytes in memory. It is not an array and supports none of the array methods and operators. You cannot read or write values from an ArrayBuffer directly; you can only check its size with the byteLength property and create slices from it using the slice() method, which returns a new ArrayBuffer.

Bun.ArrayBufferSink class

Bun.ArrayBufferSink is a fast incremental writer for constructing an ArrayBuffer of unknown size. Create an instance, call write() multiple times to add data, and call end() to retrieve the final ArrayBuffer. Supports writing strings, typed arrays, ArrayBuffer, and SharedArrayBuffer.

ArrayBufferSink basic usage example

const sink = new Bun.ArrayBufferSink(); sink.write("h"); sink.write("e"); sink.write("l"); sink.write("l"); sink.write("o"); sink.end(); // ArrayBuffer(5) [ 104, 101, 108, 108, 111 ]

ArrayBufferSink.start() options

ArrayBufferSink.start() accepts an options object with three properties: asUint8Array (boolean, returns data as Uint8Array instead of ArrayBuffer), stream (boolean, allows periodic flushing while continuing to write), and highWaterMark (number, preallocates internal buffer size in bytes for better performance with small chunks).

ArrayBufferSink asUint8Array option example

const sink = new Bun.ArrayBufferSink(); sink.start({ asUint8Array: true, }); sink.write("h"); sink.write("e"); sink.write("l"); sink.write("l"); sink.write("o"); sink.end(); // Uint8Array(5) [ 104, 101, 108, 108, 111 ]

ArrayBufferSink.write() supported types

The write() method accepts strings, typed arrays (ArrayBufferView), ArrayBuffer, and SharedArrayBuffer.

ArrayBufferSink.write() with multiple data types example

sink.write("h"); sink.write(new Uint8Array([101, 108])); sink.write(Buffer.from("lo").buffer); sink.end();

ArrayBufferSink stream mode with flush

Pass stream: true to the start() method to enable periodic flushing while continuing to write. Once end() is called, no more data can be written. In stream mode, use flush() to periodically retrieve buffered data as an ArrayBuffer (or Uint8Array if asUint8Array: true) and clear the internal buffer.

ArrayBufferSink stream mode example

const sink = new Bun.ArrayBufferSink(); sink.start({ stream: true, }); sink.write("h"); sink.write("e"); sink.write("l"); sink.flush(); // ArrayBuffer(3) [ 104, 101, 108 ] sink.write("l"); sink.write("o"); sink.flush(); // ArrayBuffer(2) [ 108, 111 ]

ArrayBufferSink highWaterMark option example

const sink = new Bun.ArrayBufferSink(); sink.start({ highWaterMark: 1024 * 1024, // 1 MB });

ArrayBufferSink API reference

ArrayBufferSink constructor: ArrayBufferSink(). Method start(options?: {asUint8Array?: boolean; highWaterMark?: number; stream?: boolean;}): void. Method write(chunk: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer): number. Method flush(): number | Uint8Array<ArrayBuffer> | ArrayBuffer (returns number if stream: false, returns Uint8Array or ArrayBuffer if stream: true). Method end(): ArrayBuffer | Uint8Array<ArrayBuffer>.

Give your agent this brain