Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/big int and other types in nested #89

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

[![Build](https://img.shields.io/npm/v/object-sizeof)](https://img.shields.io/npm/v/object-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)

### Get size of a JavaScript object in Bytes
### Get the size of a JavaScript object in Bytes

Node.js version uses the Buffer.from(objectToString) method to convert the object's string representation to a buffer, and then it uses the byteLength property to obtain the buffer size in bytes.

Module 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.
The module 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.

Please note that this function will only work in some cases, especially when dealing with complex data structures or when the object contains functions.

Expand All @@ -20,10 +20,10 @@ Please note that this function will only work in some cases, especially when dea

### Coding standards

Project follows [JavaScript Standard Style](https://standardjs.com/) as a JavaScript style guide.
Code coverage reports done using Codecov.io.
The project follows [JavaScript Standard Style](https://standardjs.com/) as a JavaScript style guide.
Code coverage reports are done using Codecov.io.

Code is written with the assumptions, that any code added, which is not tested properly, is already or will be buggy.
Code is written with the assumptions that any code added, which is not tested properly, is already or will be buggy.
Hence test coverage, with the BDD style unit tests, stating the intent, and expected behaviour, is a must.

### Get size of a JavaScript object in Bytes - version 1.x
Expand Down
64 changes: 37 additions & 27 deletions indexv2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 ChatGPT Jan 9 Version
// Copyright 2023 ChatGPT May 24 Version
/* eslint-disable new-cap */ // to fix new Buffer.from
'use strict'
const ECMA_SIZES = require('./byte_size')
Expand All @@ -24,6 +24,13 @@ function isNodeEnvironment () {
return true
}

function getSizeOfTypedArray (typedArray) {
if (typedArray.BYTES_PER_ELEMENT) {
return typedArray.length * typedArray.BYTES_PER_ELEMENT
}
return -1 // error indication
}

/**
* Size in bytes for complex objects
* @param {*} obj
Expand All @@ -32,40 +39,43 @@ function isNodeEnvironment () {
function objectSizeComplex (obj) {
let totalSize = 0
const errorIndication = -1

try {
// analyse the object to calculate it better
let potentialConversion = obj
// convert Map and Set to an object representation
let convertedObj = obj
if (obj instanceof Map) {
// convert the map to an object
potentialConversion = Object.fromEntries(obj)
convertedObj = Object.fromEntries(obj)
} else if (obj instanceof Set) {
// convert the set to an array
potentialConversion = Array.from(obj)
convertedObj = Array.from(obj)
}
if (obj instanceof Int8Array) {
return obj.length * ECMA_SIZES.Int8Array
} else if (obj instanceof Uint8Array || obj instanceof Uint8ClampedArray) {
return obj.length * ECMA_SIZES.Uint8Array
} else if (obj instanceof Int16Array) {
return obj.length * ECMA_SIZES.Int16Array
} else if (obj instanceof Uint16Array) {
return obj.length * ECMA_SIZES.Uint16Array
} else if (obj instanceof Int32Array) {
return obj.length * ECMA_SIZES.Int32Array
} else if (obj instanceof Uint32Array) {
return obj.length * ECMA_SIZES.Uint32Array
} else if (obj instanceof Float32Array) {
return obj.length * ECMA_SIZES.Float32Array
} else if (obj instanceof Float64Array) {
return obj.length * ECMA_SIZES.Float64Array

// handle typed arrays
if (ArrayBuffer.isView(obj)) {
return getSizeOfTypedArray(obj)
}
const objectToString = JSON.stringify(potentialConversion)
const buffer = new Buffer.from(objectToString)
totalSize = buffer.byteLength

const serializedObj = JSON.stringify(convertedObj, (key, value) => {
if (typeof value === 'bigint') {
return value.toString()
} else if (typeof value === 'function') {
return value.toString()
} else if (typeof value === 'undefined') {
return 'undefined'
} else if (typeof value === 'symbol') {
return value.toString()
} else if (value instanceof RegExp) {
return value.toString()
} else {
return value
}
})

totalSize = Buffer.byteLength(serializedObj, 'utf8')
} catch (ex) {
console.error('Error detected, return ' + errorIndication, ex)
console.error('Error detected, returning ' + errorIndication, ex)
return errorIndication
}

return totalSize
}

Expand Down
Loading