Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
object-sizeof
Advanced tools
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.
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
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.
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.
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.
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.
Project follows JavaScript Standard Style as a JavaScript style guide. Code coverage reports 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.
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.
npm install object-sizeof
import sizeof from 'object-sizeof'
// const sizeof = require("object-sizeof")
console.log("Object { abc: 'def' } in bytes: " + sizeof({ abc: 'def' })) // "Object { abc: 'def' } in bytes: 13"
console.log('Integer 12345 in bytes: ' + sizeof(12345)) // "Integer 12345 in bytes: 8"
The MIT License (MIT)
Copyright (c) 2015, Andrei Karpushonak aka @miktam
FAQs
Sizeof of a JavaScript object in Bytes
The npm package object-sizeof receives a total of 168,258 weekly downloads. As such, object-sizeof popularity was classified as popular.
We found that object-sizeof demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.