{"id":11482,"library":"object-sizeof","title":"Object Size Calculator","description":"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.","status":"active","version":"2.6.5","language":"javascript","source_language":"en","source_url":"git://github.com/miktam/sizeof","tags":["javascript","sizeof","size","object","bytes","typescript"],"install":[{"cmd":"npm install object-sizeof","lang":"bash","label":"npm"},{"cmd":"yarn add object-sizeof","lang":"bash","label":"yarn"},{"cmd":"pnpm add object-sizeof","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library exports a default function. Use default import syntax for ESM.","wrong":"import { sizeof } from 'object-sizeof';","symbol":"sizeof","correct":"import sizeof from 'object-sizeof';"},{"note":"CommonJS require syntax is supported and shown in examples.","symbol":"sizeof","correct":"const sizeof = require('object-sizeof');"},{"note":"The default export is a function and should be called directly, not instantiated with `new`.","wrong":"new sizeof({ a: 1 });","symbol":"sizeof","correct":"sizeof({ a: 1 });"}],"quickstart":{"code":"import sizeof from 'object-sizeof';\n\n// Basic object\nconst obj1 = { id: 1, name: 'Test Object', data: [10, 20, 30] };\nconsole.log(`Size of basic object: ${sizeof(obj1)} bytes`);\n\n// Complex object with various types\nconst complexObj = {\n  str: 'hello world!',\n  num: 123.456,\n  bool: true,\n  arr: [1, 'two', { key: 'value' }],\n  map: new Map([['a', 1], ['b', 2]]),\n  set: new Set([1, 2, 3]),\n  bigInt: BigInt(9007199254740991),\n  func: () => { console.log('I am a function'); },\n  typedArray: new Uint8Array([10, 20, 30, 40])\n};\nconsole.log(`Size of complex object: ${sizeof(complexObj)} bytes`);\n\n// Object with a circular reference (will return -1)\nconst circularObj: any = { a: 1 };\ncircularObj.b = circularObj;\nconsole.log(`Size of circular object: ${sizeof(circularObj)} bytes (expected -1 due to circularity)`);\n\n// Test with a simple number and string\nconsole.log(`Size of number 12345: ${sizeof(12345)} bytes`);\nconsole.log(`Size of string \"hello\": ${sizeof(\"hello\")} bytes`);","lang":"typescript","description":"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."},"warnings":[{"fix":"Review performance and ensure compatibility if migrating from v1.x, especially for resource-constrained environments or performance-critical applications.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.5.3"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"The input object contains a circular reference, a JSON serialization error occurred during processing, or an invalid/unrecognizable TypedArray object was passed.","error":"sizeof(...) returns -1 unexpectedly."}],"ecosystem":"npm"}