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 - version 2.x
New version uses the Buffer.from(objectToString) method to convert the string representation of the object to a buffer and then it uses the byteLength property to obtain the size of the buffer in bytes. Note that this method only work in Node.js environment.
For everything else, the calculation takes an object as an argument and 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 not work on all cases, specially when dealing with complex data structures or when the object contains functions.
Coding standards
Project uses JavaScript Standard Style. Code coverage reports done using Codecov.io.
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.
Limitations
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.
Installation
npm install object-sizeof
Examples
ES5
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))
ES6
import sizeof from 'object-sizeof'
// 2B per character, 6 chars total => 12B
console.log(sizeof({ abc: 'def' }))
// 8B for Number => 8B
console.log(sizeof(12345))
const param = {
a: 1,
b: 2,
c: {
d: 4
}
}
// 4 one two-bytes char strings and 3 eighth-bytes numbers => 32B
console.log(sizeof(param))
Licence
The MIT License (MIT)
Copyright (c) 2015, Andrei Karpushonak aka @miktam