object-sizeof
Advanced tools
Comparing version 1.6.3 to 2.0.0
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12055
10
232
88
3
+ Addedcodecov@^3.8.3
+ Addednyc@^15.1.0
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/code-frame@7.26.2(transitive)
+ Added@babel/compat-data@7.26.2(transitive)
+ Added@babel/core@7.26.0(transitive)
+ Added@babel/generator@7.26.2(transitive)
+ Added@babel/helper-compilation-targets@7.25.9(transitive)
+ Added@babel/helper-module-imports@7.25.9(transitive)
+ Added@babel/helper-module-transforms@7.26.0(transitive)
+ Added@babel/helper-string-parser@7.25.9(transitive)
+ Added@babel/helper-validator-identifier@7.25.9(transitive)
+ Added@babel/helper-validator-option@7.25.9(transitive)
+ Added@babel/helpers@7.26.0(transitive)
+ Added@babel/parser@7.26.2(transitive)
+ Added@babel/template@7.25.9(transitive)
+ Added@babel/traverse@7.25.9(transitive)
+ Added@babel/types@7.26.0(transitive)
+ Added@istanbuljs/load-nyc-config@1.1.0(transitive)
+ Added@istanbuljs/schema@0.1.3(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Added@tootallnate/once@1.1.2(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedaggregate-error@3.1.0(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedappend-transform@2.0.0(transitive)
+ Addedarchy@1.0.0(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedargv@0.0.2(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbrowserslist@4.24.2(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedcaching-transform@4.0.0(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedcaniuse-lite@1.0.30001680(transitive)
+ Addedclean-stack@2.2.0(transitive)
+ Addedcliui@6.0.0(transitive)
+ Addedcodecov@3.8.3(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcommondir@1.0.1(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconvert-source-map@1.9.02.0.0(transitive)
+ Addedcross-spawn@7.0.5(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addeddefault-require-extensions@3.0.1(transitive)
+ Addedelectron-to-chromium@1.5.59(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedes6-error@4.1.1(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedfast-url-parser@1.1.3(transitive)
+ Addedfind-cache-dir@3.3.2(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedforeground-child@2.0.0(transitive)
+ Addedfromentries@1.3.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedget-package-type@0.1.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedhasha@5.2.2(transitive)
+ Addedhtml-escaper@2.0.2(transitive)
+ Addedhttp-proxy-agent@4.0.1(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedignore-walk@3.0.4(transitive)
+ Addedimurmurhash@0.1.4(transitive)
+ Addedindent-string@4.0.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedis-windows@1.0.2(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedistanbul-lib-coverage@3.2.2(transitive)
+ Addedistanbul-lib-hook@3.0.0(transitive)
+ Addedistanbul-lib-instrument@4.0.3(transitive)
+ Addedistanbul-lib-processinfo@2.0.3(transitive)
+ Addedistanbul-lib-report@3.0.1(transitive)
+ Addedistanbul-lib-source-maps@4.0.1(transitive)
+ Addedistanbul-reports@3.1.7(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedjsesc@3.0.2(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedlodash.flattendeep@4.4.0(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedmake-dir@3.1.04.0.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addednode-preload@0.2.1(transitive)
+ Addednode-releases@2.0.18(transitive)
+ Addednyc@15.1.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-map@3.0.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpackage-hash@4.0.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpkg-dir@4.2.0(transitive)
+ Addedprocess-on-spawn@1.1.0(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedrelease-zalgo@1.0.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@2.0.0(transitive)
+ Addedresolve-from@5.0.0(transitive)
+ Addedrimraf@3.0.2(transitive)
+ Addedsemver@6.3.17.6.3(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedspawn-wrap@2.0.0(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedstream-events@1.0.5(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedstrip-bom@4.0.0(transitive)
+ Addedstubs@3.0.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedteeny-request@7.1.1(transitive)
+ Addedtest-exclude@6.0.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedtype-fest@0.8.1(transitive)
+ Addedtypedarray-to-buffer@3.1.5(transitive)
+ Addedupdate-browserslist-db@1.1.1(transitive)
+ Addedurlgrey@1.0.0(transitive)
+ Addeduuid@8.3.2(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedwrite-file-atomic@3.0.3(transitive)
+ Addedy18n@4.0.3(transitive)
+ Addedyallist@3.1.1(transitive)
+ Addedyargs@15.4.1(transitive)
+ Addedyargs-parser@18.1.3(transitive)
- Removedbuffer@5.7.1(transitive)
Updatedbuffer@^6.0.3