typestar

Buffers in JavaScript

Raw bytes, and the encodings you convert them through.

const raw = Buffer.from("typestar", "utf8");

console.log(raw.length, raw[0], raw instanceof Uint8Array);
console.log(raw.toString("hex"), raw.toString("base64"));
console.log(Buffer.from(raw.toString("base64"), "base64").toString());

const joined = Buffer.concat([Buffer.from("go "), Buffer.from("104")]);
console.log(joined.toString(), joined.subarray(0, 2).toString());

console.log(Buffer.byteLength("h\u00e9llo"), "h\u00e9llo".length);
console.log(Buffer.alloc(4).toString("hex"));

How it works

  1. Buffer.from takes a string and an encoding.
  2. toString converts back, including base64 and hex.
  3. A Buffer is a Uint8Array, so array methods work on it.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
487
Tokens
153
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 87 seconds.

Type this snippet

Step 3 of 3 in The process, step 9 of 18 in Node.js.

← Previous Next →