What is object-sizeof?
The object-sizeof npm package is used to calculate the approximate memory usage of JavaScript objects. It helps developers understand the memory footprint of their data structures, which can be crucial for optimizing performance and managing resources effectively.
What are object-sizeof's main functionalities?
Calculate size of a simple object
This feature allows you to calculate the memory size of a simple JavaScript object. The code sample demonstrates how to use the `sizeof` function to get the size of an object with basic key-value pairs.
const sizeof = require('object-sizeof');
const obj = { a: 1, b: 2, c: 3 };
console.log(sizeof(obj)); // Outputs the size of the object in bytes
Calculate size of a nested object
This feature allows you to calculate the memory size of a nested JavaScript object. The code sample shows how to use the `sizeof` function to get the size of an object with nested structures.
const sizeof = require('object-sizeof');
const nestedObj = { a: 1, b: { c: 2, d: { e: 3 } } };
console.log(sizeof(nestedObj)); // Outputs the size of the nested object in bytes
Calculate size of an array
This feature allows you to calculate the memory size of a JavaScript array. The code sample demonstrates how to use the `sizeof` function to get the size of an array.
const sizeof = require('object-sizeof');
const arr = [1, 2, 3, 4, 5];
console.log(sizeof(arr)); // Outputs the size of the array in bytes
Calculate size of a complex object
This feature allows you to calculate the memory size of a complex JavaScript object that includes various data types. The code sample shows how to use the `sizeof` function to get the size of an object with mixed data types.
const sizeof = require('object-sizeof');
const complexObj = { a: 1, b: [1, 2, 3], c: { d: 'string', e: true } };
console.log(sizeof(complexObj)); // Outputs the size of the complex object in bytes
Other packages similar to object-sizeof
sizeof
The `sizeof` package provides similar functionality to `object-sizeof` by calculating the approximate memory usage of JavaScript objects. It is straightforward to use and offers a simple API for determining the size of various data structures.
object-size
The `object-size` package is another alternative that calculates the size of JavaScript objects. It provides a similar feature set to `object-sizeof` and can be used to measure the memory footprint of different data structures.
object-sizeof
Get the size of a JavaScript object in Bytes
Node.js version uses the Buffer.from(objectToString) method to convert the object's string representation to a buffer, and then it uses the byteLength property to obtain the buffer size in bytes.
The module uses a combination of recursion and a stack to iterate through all of its properties, adding up the number of bytes for each data type it encounters.
Please note that this function will only work in some cases, especially when dealing with complex data structures or when the object contains functions.
Supported Standard built-in and complex types
- Map
- Set
- BigInt
- Function
- Typed Arrays (Int8Array, Uint32Array, Float64Array, etc)
Error handling
Errors indicated by returned -1 in following cases:
- JSON serialization error, e.g. circular references.
- Unrecognizable TypedArray object.
It prevents potential exceptions or infinite loops, improving reliability.
Coding standards
The project follows JavaScript Standard Style as a JavaScript style guide.
Code coverage reports are done using Codecov.io.
Code is written with the assumptions that any code added, which is not tested properly, is already or will be buggy.
Hence test coverage, with the BDD style unit tests, stating the intent, and expected behaviour, is a must.
Get size of a JavaScript object in Bytes - version 1.x
JavaScript does not provide sizeof (like in C), and programmer does not need to care about memory allocation/deallocation.
However, according to ECMAScript Language Specification, each String value is represented by 16-bit unsigned integer, Number uses the double-precision 64-bit format IEEE 754 values including the special "Not-a-Number" (NaN) values, positive infinity, and negative infinity.
Having this knowledge, the module calculates how much memory object will allocate.
Installation
npm install object-sizeof
Examples
const sizeof = require('object-sizeof')
const sizeObj = sizeof({ abc: 'def' })
console.log(`Size of the object: ${sizeObj} bytes`)
const sizeInt = sizeof(12345)
console.log(`Size of the int: ${sizeInt} bytes`)
Licence
The MIT License (MIT)
Copyright (c) 2015, Andrei Karpushonak aka @miktam