Registry / serialization / object-sizeof

object-sizeof

JSON →
library2.6.5jsnpmunverified

The `object-sizeof` library provides an approximation of the memory footprint of a JavaScript object in bytes. Currently at stable version 2.6.5, the package sees active maintenance with minor releases addressing types and bug fixes, as evidenced by recent updates from v2.6.1 to v2.6.4. It distinguishes itself by employing separate underlying implementations for Node.js (utilizing `Buffer.from` on stringified objects) and browser environments (using a recursive stack-based approach). A key feature is its expanded support for a wide array of built-in and complex types including Map, Set, BigInt, Function, and Typed Arrays. Unlike some alternatives, `object-sizeof` aims for robustness by returning -1 for common error scenarios such as circular references or unrecognizable TypedArray objects, preventing exceptions or infinite loops, rather than throwing errors. It also ships with TypeScript types for improved developer experience.

npm install object-sizeof
INSTALL
IMPORT
SIG · OBJECT-SIZEOF
O
object-sizeof
serializationjavascriptv2.6.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sizeof
import sizeof from 'object-sizeof';
import { sizeof } from 'object-sizeof';
The library exports a default function. Use default import syntax for ESM.
sizeof
const sizeof = require('object-sizeof');
CommonJS require syntax is supported and shown in examples.
sizeof
sizeof({ a: 1 });
new sizeof({ a: 1 });
The default export is a function and should be called directly, not instantiated with `new`.

Demonstrates calculating the approximate size of various JavaScript objects, including primitive types, complex structures like Map, Set, BigInt, Typed Arrays, functions, and showcases handling of circular references.

import sizeof from 'object-sizeof'; // Basic object const obj1 = { id: 1, name: 'Test Object', data: [10, 20, 30] }; console.log(`Size of basic object: ${sizeof(obj1)} bytes`); // Complex object with various types const complexObj = { str: 'hello world!', num: 123.456, bool: true, arr: [1, 'two', { key: 'value' }], map: new Map([['a', 1], ['b', 2]]), set: new Set([1, 2, 3]), bigInt: BigInt(9007199254740991), func: () => { console.log('I am a function'); }, typedArray: new Uint8Array([10, 20, 30, 40]) }; console.log(`Size of complex object: ${sizeof(complexObj)} bytes`); // Object with a circular reference (will return -1) const circularObj: any = { a: 1 }; circularObj.b = circularObj; console.log(`Size of circular object: ${sizeof(circularObj)} bytes (expected -1 due to circularity)`); // Test with a simple number and string console.log(`Size of number 12345: ${sizeof(12345)} bytes`); console.log(`Size of string "hello": ${sizeof("hello")} bytes`);
Debug
Known issues
breakingVersion 2.0.0 introduced separate implementations for Node.js and browser environments. The Node.js version uses `Buffer.from(objectToString)` while the browser uses a recursive stack. This is a significant internal change that may affect performance characteristics or edge case handling, especially for very large or complex objects, though the public API remains consistent.
fix
Review performance and ensure compatibility if migrating from v1.x, especially for resource-constrained environments or performance-critical applications.
affects: >=2.0.0
gotchaThe function returns -1 for specific error conditions, such as circular references within an object, JSON serialization errors, or when an unrecognizable TypedArray object is encountered. It does not throw exceptions in these scenarios.
fix
Always check the return value of `sizeof()` for -1 to identify and handle these error conditions gracefully within your application logic. Do not assume a positive byte size will always be returned.
affects: >=1.5.3
gotchaThe calculated size is an approximation of memory usage, not an exact measurement, especially for complex data structures or objects containing functions. The library specifically notes that it 'will only work in some cases' for these types.
fix
Use the result as an estimate for resource planning or general understanding, but do not rely on it for precise memory accounting. Be aware that the size of functions is based on their string representation, which is not actual allocated memory for the function's execution context.
affects: >=1.0.0
gotchaIn Node.js, the implementation relies on `Buffer.from(objectToString)`. Stringifying very large objects can be memory and CPU intensive, potentially leading to performance bottlenecks or out-of-memory errors for extremely large or deeply nested data structures.
fix
For extremely large objects, consider alternative methods or profile your application to understand the performance impact. Break down large objects if possible, or only calculate the size of relevant sub-parts.
affects: >=2.0.0
Errors
Common errors & fixes
sizeof(...) returns -1 unexpectedly.
The input object contains a circular reference, a JSON serialization error occurred during processing, or an invalid/unrecognizable TypedArray object was passed.
fix
This is the library's intended error handling mechanism to prevent exceptions. Inspect the object for circular structures, ensure it's JSON-serializable (if applicable to the internal process), and verify that any TypedArray objects are valid. Handle the -1 return value in your code as an indication of an uncalculable size.
Upgrade
Version history
2.6.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
51 hits · last 30 days
node
50
Amazon
1
Resources
object-sizeof — npm install object-sizeof · libregistry