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-sizeofVerified import paths — ran on the pinned version, not inferred.
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.
Review performance and ensure compatibility if migrating from v1.x, especially for resource-constrained environments or performance-critical applications.
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.
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.
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.
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.
No dependency data recorded yet.