Any value to measure. Primitives, objects, arrays, null, and undefined are all accepted.
Optionalaccuracy: JsonByteSizeAccuracy = 'exact'Controls the precision/speed trade-off. Defaults to 'exact'.
The UTF-8 byte length of the JSON-serialized value.
// Basic usage — measures the exact byte size of a serialized object
jsonByteSize({ name: 'Alice', age: 30 }); // Returns 22
// Fast mode — same result for ASCII data, avoids allocating the full JSON string
jsonByteSize({ name: 'Alice', age: 30 }, 'fast'); // Returns 22
Returns the UTF-8 byte size of a value as it would appear when JSON-serialized.
The
accuracyparameter controls the precision/speed trade-off:'exact'(default) — Serializes the value via safeJsonStringify and measures the resulting string. Handles every edge case (circular references, throwing getters,toJSON(),BigInt, etc.) at the cost of O(n) memory for the intermediate JSON string.'fast'— Recursive walk using real UTF-8 byte counts plus JSON escape overhead. CallstoJSON()and skips throwing getters. Does not protect against circular references.'estimate'— Like'fast'but uses character count (string.length) instead of UTF-8 byte counts, and does not calltoJSON(). Fastest option but may undercount multi-byte Unicode strings or strings with many escape sequences.