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
Buffer.fromtakes a string and an encoding.toStringconverts back, including base64 and hex.- A Buffer is a Uint8Array, so array methods work on it.
Keywords and builtins used here
Uint8Arrayconstfrom
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.
Step 3 of 3 in The process, step 9 of 18 in Node.js.