Object Size Calculator
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.
Common errors
-
sizeof(...) returns -1 unexpectedly.
cause The input object contains a circular reference, a JSON serialization error occurred during processing, or an invalid/unrecognizable TypedArray object was passed.fixThis 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.
Warnings
- breaking Version 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.
- gotcha The 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.
- gotcha The 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.
- gotcha In 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.
Install
-
npm install object-sizeof -
yarn add object-sizeof -
pnpm add object-sizeof
Imports
- sizeof
import { sizeof } from 'object-sizeof';import sizeof from 'object-sizeof';
- sizeof
const sizeof = require('object-sizeof'); - sizeof
new sizeof({ a: 1 });sizeof({ a: 1 });
Quickstart
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`);