Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-sizeof

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-sizeof - npm Package Compare versions

Comparing version 1.6.3 to 2.0.0

indexv2.d.ts

1

byte_size.js

@@ -10,3 +10,4 @@ /**

BOOLEAN: 4,
BYTES: 4,
NUMBER: 8
}

41

index.js

@@ -5,13 +5,16 @@ // Copyright 2014 Andrei Karpushonak

var ECMA_SIZES = require('./byte_size')
var Buffer = require('buffer/').Buffer
const ECMA_SIZES = require('./byte_size')
const Buffer = require('buffer/').Buffer
function allProperties(obj) {
const isNodePlatform =
typeof process === 'object' && typeof require === 'function'
function allProperties (obj) {
const stringProperties = []
for (var prop in obj) {
stringProperties.push(prop)
for (const prop in obj) {
stringProperties.push(prop)
}
if (Object.getOwnPropertySymbols) {
var symbolProperties = Object.getOwnPropertySymbols(obj)
Array.prototype.push.apply(stringProperties, symbolProperties)
const symbolProperties = Object.getOwnPropertySymbols(obj)
Array.prototype.push.apply(stringProperties, symbolProperties)
}

@@ -26,6 +29,6 @@ return stringProperties

var bytes = 0
var properties = allProperties(object)
for (var i = 0; i < properties.length; i++) {
var key = properties[i]
let bytes = 0
const properties = allProperties(object)
for (let i = 0; i < properties.length; i++) {
const key = properties[i]
// Do not recalculate circular references

@@ -55,3 +58,3 @@ if (typeof object[key] === 'object' && object[key] !== null) {

function getCalculator (seen) {
return function calculator(object) {
return function calculator (object) {
if (Buffer.isBuffer(object)) {

@@ -61,6 +64,9 @@ return object.length

var objectType = typeof (object)
const objectType = typeof object
switch (objectType) {
case 'string':
return object.length * ECMA_SIZES.STRING
// https://stackoverflow.com/questions/68789144/how-much-memory-do-v8-take-to-store-a-string/68791382#68791382
return isNodePlatform
? 12 + 4 * Math.ceil(object.length / 4)
: object.length * ECMA_SIZES.STRING
case 'boolean':

@@ -70,5 +76,8 @@ return ECMA_SIZES.BOOLEAN

return ECMA_SIZES.NUMBER
case 'symbol':
case 'symbol': {
const isGlobalSymbol = Symbol.keyFor && Symbol.keyFor(object)
return isGlobalSymbol ? Symbol.keyFor(object).length * ECMA_SIZES.STRING : (object.toString().length - 8) * ECMA_SIZES.STRING
return isGlobalSymbol
? Symbol.keyFor(object).length * ECMA_SIZES.STRING
: (object.toString().length - 8) * ECMA_SIZES.STRING
}
case 'object':

@@ -75,0 +84,0 @@ if (Array.isArray(object)) {

{
"name": "object-sizeof",
"version": "1.6.3",
"version": "2.0.0",
"description": "Sizeof of a JavaScript object in Bytes",
"main": "index.js",
"main": "indexv2.js",
"scripts": {
"test": "./node_modules/.bin/standard; ./node_modules/.bin/mocha -R tap"
"test": "./node_modules/.bin/standard; ./node_modules/.bin/mocha",
"lint": "./node_modules/.bin/standard",
"coverage": "./node_modules/.bin/nyc npm test; ./node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov && ./node_modules/.bin/codecov --token=708ee89f-4e9c-402d-8380-1257b979596c"
},

@@ -23,10 +25,12 @@ "repository": {

"dependencies": {
"buffer": "^5.6.0"
"buffer": "^6.0.3",
"codecov": "^3.8.3",
"nyc": "^15.1.0"
},
"devDependencies": {
"mocha": "^9.2.0",
"mocha": "^10.2.0",
"should": "^13.2.3",
"standard": "^16.0.4"
"standard": "^17.0.0"
},
"types": "index.d.ts"
"types": "indexv2.d.ts"
}
## object-sizeof
[![Build Status](https://travis-ci.org/miktam/sizeof.svg?branch=master)](https://travis-ci.org/miktam/sizeof) [![Dependency Status](https://david-dm.org/miktam/sizeof.svg)](https://david-dm.org/miktam/sizeof)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof?ref=badge_shield)
[![Build Status](https://travis-ci.org/miktam/sizeof.svg?branch=master)](https://travis-ci.org/miktam/sizeof) ![GitHub contributors](https://img.shields.io/github/contributors/miktam/sizeof) [![NPM](https://img.shields.io/npm/dy/object-sizeof)](https://img.shields.io/npm/dy/object-sizeof) [![codecov](https://codecov.io/gh/miktam/sizeof/branch/master/graph/badge.svg?token=qPHxmWpC1K)](https://codecov.io/gh/miktam/sizeof)
[![NPM](https://nodei.co/npm/object-sizeof.png?downloads=true&downloadRank=true)](https://nodei.co/npm/object-sizeof/)
### Get size of a JavaScript object in Bytes - version 2.x
### Get size of a JavaScript object in Bytes
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.
JavaScript does not provide sizeof (like in C), and programmer does not need to care about memory allocation/deallocation.
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.
However, according to [ECMAScript Language Specification](http://www.ecma-international.org/ecma-262/5.1/), 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.
Please note that this function will not work on all cases, specially when dealing with complex data structures or when the object contains functions.
Having this knowledge, the module calculates how much memory object will allocate.
### Coding standards
Project uses [JavaScript Standard Style](https://standardjs.com/).
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](http://www.ecma-international.org/ecma-262/5.1/), 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.
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

@@ -28,19 +40,19 @@

```javascript
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
}
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))
}
// 4 one two-bytes char strings and 3 eighth-bytes numbers => 32B
console.log(sizeof(param))
```

@@ -51,19 +63,19 @@

```javascript
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
}
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))
}
// 4 one two-bytes char strings and 3 eighth-bytes numbers => 32B
console.log(sizeof(param))
```

@@ -77,2 +89,2 @@

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof?ref=badge_large)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof?ref=badge_shield)

@@ -5,9 +5,8 @@ 'use strict'

var should = require('should')
var sizeof = require('../index')
const Buffer = require('buffer/').Buffer
const should = require('should')
const sizeof = require('../indexv2.js')
describe('sizeof', function () {
it('should handle null in object keys', function () {
var badData = { 1: { depot_id: null, hierarchy_node_id: null } }
const badData = { 1: { depot_id: null, hierarchy_node_id: null } }
sizeof(badData).should.be.instanceOf(Number)

@@ -28,10 +27,6 @@ })

it('of 3 chars string is 2*3=6', function () {
it('of 3 chars string is 6', function () {
sizeof('abc').should.be.equal(6)
})
it('simple object of 3 chars for key and value', function () {
sizeof({ abc: 'def' }).should.be.equal(2 * 3 * 2)
})
it('boolean size shall be 4', function () {

@@ -41,26 +36,5 @@ sizeof(true).should.be.equal(4)

it('buffer size should be correct', function () {
sizeof(Buffer.alloc(3)).should.be.equal(3)
})
it('nested objects shall be counted in full', function () {
// 4 one two-bytes char strings and 3 eighth-bytes numbers
var param = { a: 1, b: 2, c: { d: 4 } }
sizeof(param).should.be.equal(4 * 2 + 3 * 8)
})
it('object with 100 three-chars keys and values as numbers => 100 * 2 * 3 + 100 * 8', function () {
var obj = {}
var ELEMENTS = 100
// start from 1M to have the same keys length
for (var i = 100; i < 100 + ELEMENTS; i++) {
obj[i] = i
}
sizeof(obj).should.be.equal(ELEMENTS * 2 * (('' + ELEMENTS).length) + ELEMENTS * 8)
})
it('report an error for circular dependency objects', function () {
var firstLevel = { a: 1 }
var secondLevel = { b: 2, c: firstLevel }
const firstLevel = { a: 1 }
const secondLevel = { b: 2, c: firstLevel }
firstLevel.second = secondLevel

@@ -76,14 +50,2 @@ should.exist(sizeof(firstLevel))

it('array support for strings', function () {
sizeof(['a', 'b', 'c', 'd']).should.be.equal(8)
})
it('array support for numbers', function () {
sizeof([1, 2, 3, 4]).should.equal(32)
})
it('array support for NaN', function () {
sizeof([null, undefined, 3, 4]).should.equal(16)
})
it('supports symbol', () => {

@@ -94,41 +56,7 @@ const descriptor = 'abcd'

it('supports symbols as keys', () => {
const descriptor = 'abcd'
const symbol = Symbol(descriptor)
const value = 'efg'
sizeof({ [symbol]: value }).should.equal(2 * descriptor.length + 2 * value.length)
})
it('supports nested symbols as keys', () => {
const a = Symbol('a')
const b = Symbol('b')
const c = Symbol('c')
const obj = { [a]: { [b]: { [c]: 'd' } } }
sizeof(obj).should.equal(8)
})
it('supports nested symbols as values', () => {
const a = Symbol('a')
const b = Symbol('b')
const c = Symbol('c')
const d = Symbol('d')
const obj = { [a]: { [b]: { [c]: d } } }
sizeof(obj).should.equal(8)
})
it('does not recount seen objects', () => {
const a = Symbol('a')
const b = Symbol('b')
const c = Symbol('c')
const d = Symbol('d')
const obj = { [a]: { [b]: { [c]: d } } }
obj[Symbol()] = obj[a]
sizeof(obj).should.equal(8)
})
it('supports global symbols', () => {
const globalSymbol = Symbol.for('a')
const obj = { [globalSymbol]: 'b'}
sizeof(obj).should.equal(4)
const obj = { [globalSymbol]: 'b' }
sizeof(obj).should.equal(2)
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc