Package Exports
- object-sizeof
- object-sizeof/indexv2.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-sizeof) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
object-sizeof
Get 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.
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
Coding standards
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.
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
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"
Licence
The MIT License (MIT)
Copyright (c) 2015, Andrei Karpushonak aka @miktam