object-sizeof
Advanced tools
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" |
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
8264
145