jsrosetta

Collections

Buffer

Node.js's Buffer.alloc, writeUIntBE/LE, and Buffer.compare compared to Go's []byte, Rust's &mut [u8], Swift's [UInt8], and Java's byte[].

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 3.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Node.js's Buffer is a high-level API for working with binary data, with built-in methods for reading and writing big-endian and little-endian values. Go has no dedicated Buffer type — you operate directly on []byte using the standard encoding/binary, encoding/hex, and bytes packages. Rust, Swift, and Java have no built-in writeUIntBE/writeUIntLE-style method for arbitrary byte lengths either — all three hand-write it with bit shifting (>>, &), the same way Go does.

Writing integers as big-endian / little-endian

const buf = Buffer.alloc(6)
 
const value = 0x1234567890ab
buf.writeUIntBE(value, 0, 6)
console.log(buf.toString('hex')) // 1234567890ab
 
const buf2 = Buffer.alloc(6)
buf2.writeUIntLE(value, 0, 6)
console.log(buf2.toString('hex')) // ab9078563412

Comparing two buffers

let isEqual = Buffer.compare(buf, buf2) === 0
console.log(isEqual) // false
 
isEqual = Buffer.compare(buf, buf) === 0
console.log(isEqual) // true

Reference: github.com/miguelmota/golang-for-nodejs-developers#buffers