JS Object to Node.js Buffer Converter
Convert JavaScript objects to Node.js-compatible binary buffers using typed schemas. Supports all data types: int8-32, float, double, string, boolean, nested objects & arrays.
About
Binary serialization of JavaScript objects requires a rigid schema that maps each property to a fixed-width data type. Without a predefined scheme, you cannot reconstruct the original object from raw bytes - there are no delimiters, no field names, no type markers in the output buffer. This tool implements the exact serialization protocol used by the data-struct npm package: each primitive is written at its native byte width (1 byte for boolean/int8/uint8, 2 bytes for int16/uint16, 4 bytes for int32/uint32/float, 8 bytes for double), strings carry a 2-byte length prefix encoding the UTF-8 byte count (not character count), and arrays carry a 2-byte element count prefix. Nesting is resolved by recursive descent - the schema mirrors the object graph exactly.
Mismatched schemas produce silent data corruption, not exceptions. A uint16 field read as int32 shifts every subsequent offset by 2 bytes, cascading into garbage output. This tool validates type ranges before writing (e.g., clamping uint8 to 0 - 255) and flags overflows. It operates entirely in the browser using DataView and Uint8Array, producing output byte-identical to Node.js Buffer. Limitation: floating-point values follow IEEE 754 and may exhibit rounding artifacts at extreme precision.
Formulas
The total buffer size for a flat object with k fields is the sum of each field's byte footprint:
For variable-length types, the byte footprint includes the header:
Where n is the number of elements in the array and byteLength returns the UTF-8 encoded byte count (not the JavaScript .length character count). Multi-byte Unicode characters (e.g., emoji) consume 3 - 4 bytes per character. The 2-byte header for strings and shortBuffers limits payload to 216 − 1 = 65535 bytes. The 4-byte header for buffer type allows up to 232 − 1 = 4294967295 bytes.
For nested objects, recursion applies: size(object) = k∑i=1 size(fieldi). No overhead bytes are added for object nesting itself - only the constituent fields contribute to size.
Reference Data
| Type Name | Byte Size | Value Range | JS Read Method | JS Write Method | Notes |
|---|---|---|---|---|---|
| boolean | 1 | 0 - 1 | getUint8 | setUint8 | 0 = FALSE, any other = TRUE |
| int8 | 1 | −128 to 127 | getInt8 | setInt8 | Signed byte |
| uint8 | 1 | 0 to 255 | getUint8 | setUint8 | Unsigned byte |
| int16 | 2 | −32768 to 32767 | getInt16 | setInt16 | Signed short |
| uint16 | 2 | 0 to 65535 | getUint16 | setUint16 | Unsigned short, used for array length headers |
| int32 | 4 | −2147483648 to 2147483647 | getInt32 | setInt32 | Signed integer |
| uint32 | 4 | 0 to 4294967295 | getUint32 | setUint32 | Unsigned integer |
| float | 4 | ±3.4 × 1038 | getFloat32 | setFloat32 | IEEE 754 single precision, ~7 significant digits |
| double | 8 | ±1.8 × 10308 | getFloat64 | setFloat64 | IEEE 754 double precision, ~15 significant digits |
| string | 2 + n | Max 65535 bytes | TextDecoder | TextEncoder | Header is uint16 byte length, UTF-8 encoded |
| shortBuffer | 2 + n | Max 65535 bytes | slice | set | Raw binary with uint16 length header |
| buffer | 4 + n | Max 4294967295 bytes | slice | set | Raw binary with uint32 length header |
| array | 2 + n × element | Max 65535 elements | loop | loop | Header is uint16 element count, not byte count |