diff --git a/README.md b/README.md index 3e2248f..dd1c5b3 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,21 @@ [![Build](https://img.shields.io/npm/v/object-sizeof)](https://img.shields.io/npm/v/object-sizeof) [![Build Status](https://travis-ci.org/miktam/sizeof.svg?branch=master)](https://travis-ci.org/miktam/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 - Node.js +### Get 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. -### Standard built in types support +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. + +### Supported Standard built-in and complex types - Map - Set - BigInt - -### Get size of a JavaScript object in Bytes - Browser - -For the browser, the calculation takes an object as an argument. It 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. - +- Function + ### Coding standards Project follows [JavaScript Standard Style](https://standardjs.com/) as a JavaScript style guide. @@ -34,10 +33,6 @@ However, according to [ECMAScript Language Specification](http://www.ecma-intern Having this knowledge, the module calculates how much memory object will allocate. -### Limitations - -Please note, that V8 which compiles the JavaScript into native machine code, is not taken into account, as the compiled code is additionally heavily optimized. - ### Installation `npm install object-sizeof` diff --git a/indexv2.js b/indexv2.js index 42c68ab..7c6ccfa 100644 --- a/indexv2.js +++ b/indexv2.js @@ -1,14 +1,15 @@ // Copyright 2023 ChatGPT Jan 9 Version /* eslint-disable new-cap */ // to fix new Buffer.from 'use strict' +const util = require('util') const ECMA_SIZES = require('./byte_size') /** - * Size in bytes in a Node.js environment + * Size in bytes for complex objects * @param {*} obj * @returns size in bytes, or -1 if JSON.stringify threw an exception */ -function objectSizeNode (obj) { +function objectSizeComplex (obj) { let totalSize = 0 const errorIndication = -1 try { @@ -32,11 +33,11 @@ function objectSizeNode (obj) { } /** - * Size in bytes in a browser environment + * Size in bytes for primitive types * @param {*} obj * @returns size in bytes */ -function objectSizeBrowser (obj) { +function objectSizeSimple (obj) { const objectList = [] const stack = [obj] let bytes = 0 @@ -59,6 +60,8 @@ function objectSizeBrowser (obj) { } } else if (typeof value === 'bigint') { bytes += Buffer.from(value.toString()).byteLength + } else if (typeof value === 'function') { + bytes += Buffer.byteLength(util.inspect(value), 'utf8') } else if (typeof value === 'object' && objectList.indexOf(value) === -1) { objectList.push(value) @@ -70,28 +73,13 @@ function objectSizeBrowser (obj) { return bytes } -/** - * Are we running in a Node.js environment - * @returns boolean - */ -function isNodeEnvironment () { - if ( - typeof process !== 'undefined' && - process.versions && - process.versions.node - ) { - return true - } - return false -} - module.exports = function (obj) { let totalSize = 0 - if (obj !== null && typeof obj === 'object' && isNodeEnvironment()) { - totalSize = objectSizeNode(obj) + if (obj !== null && typeof obj === 'object') { + totalSize = objectSizeComplex(obj) } else { - totalSize = objectSizeBrowser(obj) + totalSize = objectSizeSimple(obj) } return totalSize diff --git a/package.json b/package.json index e67fe2f..13a52d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "object-sizeof", - "version": "2.3.0", + "version": "2.4.0", "description": "Sizeof of a JavaScript object in Bytes", "main": "indexv2.js", "scripts": { diff --git a/test/test.js b/test/test.js index 890dedc..c7b7507 100644 --- a/test/test.js +++ b/test/test.js @@ -91,4 +91,18 @@ describe('sizeof', () => { it('BigInt support', () => { sizeof(BigInt(21474836480)).should.equal(11) }) + + it('Function support', () => { + const func = (one, two) => { + return one + two + } + sizeof(func).should.equal(16) + }) + + it('nested objects', () => { + const obj = { a: 1, b: 2, c: 3 } + sizeof(obj).should.be.equal(19) + const nested = { d: obj } + sizeof(nested).should.be.equal(25) + }) })