Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
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.
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.
Please note, that V8 which compiles the JavaScript into native machine code, is not taken into account, as the compiled code is additionally heavily optimized.
npm install object-sizeof
var sizeof = require('object-sizeof');
// 2B per character, 6 chars total => 12B
console.log(sizeof({abc: 'def'}));
// 8B for Number => 8B
console.log(sizeof(12345);
var param = {
'a': 1,
'b': 2,
'c': {
'd': 4
}
};
// 4 one two-bytes char strings and 3 eighth-bytes numbers => 32B
console.log(sizeof(param));
The MIT License (MIT)
Copyright (c) 2015, Andrei Karpushonak aka @miktam, http://avrora.io
FAQs
Sizeof of a JavaScript object in Bytes
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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
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.