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.3.0 to 1.3.1

8

byte_size.js

@@ -8,5 +8,5 @@ /**

module.exports = {
STRING: 2,
BOOLEAN: 4,
NUMBER: 8
};
STRING: 2,
BOOLEAN: 4,
NUMBER: 8
}
// Copyright 2014 Andrei Karpushonak
"use strict";
'use strict'
var ECMA_SIZES = require('./byte_size');
var Buffer = require('buffer').Buffer;
var ECMA_SIZES = require('./byte_size')
var Buffer = require('buffer').Buffer
function sizeOfObject (object) {
if (object == null) {
return 0
}
var bytes = 0
for (var key in object) {
if (!Object.hasOwnProperty.call(object, key)) {
continue
}
bytes += sizeof(key)
try {
bytes += sizeof(object[key])
} catch (ex) {
if (ex instanceof RangeError) {
// circular reference detected, final result might be incorrect
// let's be nice and not throw an exception
bytes = 0
}
}
}
return bytes
}
/**

@@ -14,39 +40,22 @@ * Main module's entry point

*/
function sizeof(object) {
if (object !== null && typeof (object) === 'object') {
if (Buffer.isBuffer(object)) {
return object.length;
}
else {
var bytes = 0;
for (var key in object) {
function sizeof (object) {
if (Buffer.isBuffer(object)) {
return object.length
}
if(!Object.hasOwnProperty.call(object, key)) {
continue;
}
bytes += sizeof(key);
try {
bytes += sizeof(object[key]);
} catch (ex) {
if(ex instanceof RangeError) {
// circular reference detected, final result might be incorrect
// let's be nice and not throw an exception
bytes = 0;
}
}
}
return bytes;
}
} else if (typeof (object) === 'string') {
return object.length * ECMA_SIZES.STRING;
} else if (typeof (object) === 'boolean') {
return ECMA_SIZES.BOOLEAN;
} else if (typeof (object) === 'number') {
return ECMA_SIZES.NUMBER;
} else {
return 0;
}
var objectType = typeof (object)
switch (objectType) {
case 'string':
return object.length * ECMA_SIZES.STRING
case 'boolean':
return ECMA_SIZES.BOOLEAN
case 'number':
return ECMA_SIZES.NUMBER
case 'object':
return sizeOfObject(object)
default:
return 0
}
}
module.exports = sizeof;
module.exports = sizeof
{
"name": "object-sizeof",
"version": "1.3.0",
"version": "1.3.1",
"description": "Sizeof of a JavaScript object in Bytes",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "./node_modules/.bin/mocha -R tap"
"test": "./node_modules/.bin/standard; ./node_modules/.bin/mocha -R tap"
},

@@ -28,4 +27,5 @@ "repository": {

"mocha": "^5.2.0",
"should": "^13.2.3"
"should": "^13.2.3",
"standard": "^12.0.1"
}
}

@@ -7,4 +7,2 @@ ## object-sizeof

[![NPM](https://nodei.co/npm-dl/object-sizeof.png)](https://nodei.co/npm-dl/object-sizeof/)
### Get size of a JavaScript object in Bytes

@@ -28,9 +26,9 @@

```javascript
var sizeof = require('object-sizeof');
var sizeof = require('object-sizeof')
// 2B per character, 6 chars total => 12B
console.log(sizeof({abc: 'def'}));
console.log(sizeof({abc: 'def'}))
// 8B for Number => 8B
console.log(sizeof(12345));
console.log(sizeof(12345))

@@ -43,5 +41,5 @@ var param = {

}
};
}
// 4 one two-bytes char strings and 3 eighth-bytes numbers => 32B
console.log(sizeof(param));
console.log(sizeof(param))

@@ -48,0 +46,0 @@ ```

@@ -1,73 +0,71 @@

"use strict";
'use strict'
/*global describe, it, before, beforeEach, after, afterEach */
/* global describe, it */
var should = require('should');
var sizeof = require("../index");
var should = require('should')
var sizeof = require('../index')
describe('sizeof', function() {
describe('sizeof', function () {
it('should handle null in object keys', function () {
var badData = { '1': { 'depot_id': null, 'hierarchy_node_id': null } }
sizeof(badData).should.be.instanceOf(Number)
})
it('should handle null in object keys', function() {
var badData = {"1":{"depot_id":null,"hierarchy_node_id":null}};
sizeof(badData).should.be.instanceOf(Number);
});
it('null is 0', function () {
sizeof(null).should.be.equal(0)
})
it('null is 0', function() {
sizeof(null).should.be.equal(0);
});
it('number size shall be 8', function () {
sizeof(5).should.be.equal(8)
})
it('number size shall be 8', function() {
sizeof(5).should.be.equal(8);
});
it('undefined is 0', function () {
sizeof().should.be.equal(0)
})
it('undefined is 0', function() {
sizeof().should.be.equal(0);
});
it('of 3 chars string is 2*3=6', function () {
sizeof('abc').should.be.equal(6)
})
it('of 3 chars string is 2*3=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('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 () {
sizeof(true).should.be.equal(4)
})
it('boolean size shall be 4', function() {
sizeof(true).should.be.equal(4);
});
it('buffer size should be correct', function() {
sizeof(new Buffer(3)).should.be.equal(3);
});
it('buffer size should be correct', function () {
sizeof(Buffer.alloc(3)).should.be.equal(3)
})
it('nested objects shall be counted in full', function() {
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);
});
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;
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;
for (var i = 100; i < 100 + ELEMENTS; i++) {
obj[i] = i
}
sizeof(obj).should.be.equal(ELEMENTS * 2 * (('' + ELEMENTS).length) + ELEMENTS * 8);
});
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};
firstLevel.second = secondLevel;
should.exist(sizeof(firstLevel));
});
it('report an error for circular dependency objects', function () {
var firstLevel = { a: 1 }
var secondLevel = { b: 2, c: firstLevel }
firstLevel.second = secondLevel
should.exist(sizeof(firstLevel))
})
it('handle hasOwnProperty key', function() {
sizeof({hasOwnProperty: undefined}).should.be.instanceOf(Number);
sizeof({hasOwnProperty: 'Hello World'}).should.be.instanceOf(Number);
sizeof({hasOwnProperty: 1234}).should.be.instanceOf(Number);
});
});
it('handle hasOwnProperty key', function () {
sizeof({ hasOwnProperty: undefined }).should.be.instanceOf(Number)
sizeof({ hasOwnProperty: 'Hello World' }).should.be.instanceOf(Number)
sizeof({ hasOwnProperty: 1234 }).should.be.instanceOf(Number)
})
})

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