Skip to content

Commit

Permalink
Added function support (#76)
Browse files Browse the repository at this point in the history
* Added function support. Fixes #66

* Drop node.js and browser differentiation
  • Loading branch information
miktam authored Jan 29, 2023
1 parent 18e5d95 commit 5dbd962
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`
Expand Down
32 changes: 10 additions & 22 deletions indexv2.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 5dbd962

Please sign in to comment.