aboutsummaryrefslogtreecommitdiff
path: root/src/helpers/memorySize.js
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2022-01-19 13:19:26 -0500
committerPriyansh <[email protected]>2022-01-19 13:19:26 -0500
commit6af5afc1b0d3b08b731bead0a4e2cad27dfd472c (patch)
tree01adfbdfc7b57ce9b2cc8c85985782a6524bdc21 /src/helpers/memorySize.js
parent420085e4a3ab242523b462fd12a2d07c1693f2aa (diff)
downloadizuku.js-6af5afc1b0d3b08b731bead0a4e2cad27dfd472c.tar.xz
izuku.js-6af5afc1b0d3b08b731bead0a4e2cad27dfd472c.zip
feat: info functions
Diffstat (limited to 'src/helpers/memorySize.js')
-rw-r--r--src/helpers/memorySize.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/helpers/memorySize.js b/src/helpers/memorySize.js
new file mode 100644
index 0000000..044681a
--- /dev/null
+++ b/src/helpers/memorySize.js
@@ -0,0 +1,96 @@
+// eslint-disable-next-line @typescript-eslint/no-var-requires
+const Buffer = require('buffer/').Buffer;
+
+const ECMA_SIZES = {
+ STRING: 2,
+ BOOLEAN: 4,
+ NUMBER: 8
+};
+
+function allProperties(obj) {
+ const stringProperties = [];
+ for (var prop in obj) {
+ stringProperties.push(prop);
+ }
+ if (Object.getOwnPropertySymbols) {
+ var symbolProperties = Object.getOwnPropertySymbols(obj);
+ Array.prototype.push.apply(stringProperties, symbolProperties);
+ }
+ return stringProperties;
+}
+
+function sizeOfObject(seen, object) {
+ if (object == null) {
+ return 0;
+ }
+
+ var bytes = 0;
+ var properties = allProperties(object);
+ for (var i = 0; i < properties.length; i++) {
+ var key = properties[i];
+ // Do not recalculate circular references
+ if (typeof object[key] === 'object' && object[key] !== null) {
+ if (seen.has(object[key])) {
+ continue;
+ }
+ seen.add(object[key]);
+ }
+
+ bytes += getCalculator(seen)(key);
+ try {
+ bytes += getCalculator(seen)(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;
+}
+
+function getCalculator(seen) {
+ return function calculator(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 'symbol':
+ // eslint-disable-next-line no-case-declarations
+ const isGlobalSymbol = Symbol.keyFor && Symbol.keyFor(object);
+ return isGlobalSymbol
+ ? Symbol.keyFor(object).length * ECMA_SIZES.STRING
+ : (object.toString().length - 8) * ECMA_SIZES.STRING;
+ 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;
+ }
+ };
+}
+
+/**
+ * Main module's entry point
+ * Calculates Bytes for the provided parameter
+ * @param object - handles object/string/boolean/buffer
+ * @returns {*}
+ */
+export function sizeof(object) {
+ return getCalculator(new WeakSet())(object);
+}