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.5.2 to 1.5.3

66

index.js

@@ -8,3 +8,3 @@ // Copyright 2014 Andrei Karpushonak

function sizeOfObject (object) {
function sizeOfObject (seen, object) {
if (object == null) {

@@ -16,9 +16,13 @@ return 0

for (var key in object) {
if (!Object.hasOwnProperty.call(object, key)) {
continue
// Do not recalculate circular references
if (typeof object[key] === 'object' && object[key] !== null) {
if (seen.has(object[key])) {
continue
}
seen.add(object[key])
}
bytes += sizeof(key)
bytes += getCalculator(seen)(key)
try {
bytes += sizeof(object[key])
bytes += getCalculator(seen)(object[key])
} catch (ex) {

@@ -36,2 +40,30 @@ if (ex instanceof RangeError) {

function getCalculator (seen) {
return function (object) {
if (Buffer.isBuffer(object)) {
return object.length
}
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':
if (Array.isArray(object)) {
return object.map(getCalculator(seen)).reduce(function (acc, curr) {
return acc + curr
}, 0)
} else {
return sizeOfObject(seen, object)
}
default:
return 0
}
}
}
/**

@@ -44,27 +76,5 @@ * Main module's entry point

function sizeof (object) {
if (Buffer.isBuffer(object)) {
return object.length
}
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':
if (Array.isArray(object)) {
return object.map(sizeof).reduce(function (acc, curr) {
return acc + curr
}, 0)
} else {
return sizeOfObject(object)
}
default:
return 0
}
return getCalculator(new WeakSet())(object)
}
module.exports = sizeof
{
"name": "object-sizeof",
"version": "1.5.2",
"version": "1.5.3",
"description": "Sizeof of a JavaScript object in Bytes",

@@ -26,3 +26,3 @@ "main": "index.js",

"devDependencies": {
"mocha": "^6.2.1",
"mocha": "^7.0.1",
"should": "^13.2.3",

@@ -29,0 +29,0 @@ "standard": "^14.3.1"

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