Skip to content

Commit

Permalink
Set support (#74)
Browse files Browse the repository at this point in the history
* Added Set support
  • Loading branch information
miktam authored Jan 28, 2023
1 parent b7d0777 commit 42a0c05
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 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)
[![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

Expand All @@ -9,18 +9,22 @@ Node.js version uses the Buffer.from(objectToString) method to convert the objec
### Complex types support

- Map
- Set

### Get size of a JavaScript object in Bytes - Browser
### 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.

### Coding standards

Project uses [JavaScript Standard Style](https://standardjs.com/).
Project follows [JavaScript Standard Style](https://standardjs.com/) as a JavaScript style guide.
Code coverage reports 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.
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

JavaScript does not provide sizeof (like in C), and programmer does not need to care about memory allocation/deallocation.
Expand All @@ -41,22 +45,9 @@ Please note, that V8 which compiles the JavaScript into native machine code, is

```javascript
import sizeof from 'object-sizeof'

// 2B per character, 6 chars total => 12B
console.log(sizeof({ abc: 'def' }))

// 8B for Number => 8B
console.log(sizeof(12345))

const param = {
a: 1,
b: 2,
c: {
d: 4
}
}
// 4 one two-bytes char strings and 3 eighth-bytes numbers => 32B
console.log(sizeof(param))
// const sizeof = require("object-sizeof")
console.log("Object { abc: 'def' } in bytes: " + sizeof({ abc: 'def' })) // "Object { abc: 'def' } in bytes: 13"
console.log("Integer 12345 in bytes: " + sizeof(12345)) // "Integer 12345 in bytes: 8"
```

### Licence
Expand Down
5 changes: 4 additions & 1 deletion indexv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ function objectSizeNode (obj) {
// analyse the object to calculate it better
let potentialConversion = obj
if (obj instanceof Map) {
// convert the map to an object, including the nested properties
// convert the map to an object
potentialConversion = Object.fromEntries(obj)
} else if (obj instanceof Set) {
// convert the set to an array
potentialConversion = Array.from(obj)
}
const objectToString = JSON.stringify(potentialConversion)
const buffer = new Buffer.from(objectToString)
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.1.0",
"version": "2.2.0",
"description": "Sizeof of a JavaScript object in Bytes",
"main": "indexv2.js",
"scripts": {
Expand Down
13 changes: 12 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,23 @@ describe('sizeof', () => {
})

it('map support', () => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
const mapSmaller = new Map()
mapSmaller.set('a', 1)
const mapBigger = new Map()
mapBigger.set('a', 1)
mapBigger.set('b', 2)
console.log(sizeof(mapBigger), sizeof(mapSmaller))
sizeof(mapBigger).should.be.above(sizeof(mapSmaller))
})

it('set support', () => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
const smallerSet = new Set()
smallerSet.add(1) // Set(1) { 1 }

const biggerSet = new Set()
biggerSet.add(1) // Set(1) { 1 }
biggerSet.add('some text') // Set(3) { 1, 5, 'some text' }
sizeof(biggerSet).should.be.above(sizeof(smallerSet))
})
})

0 comments on commit 42a0c05

Please sign in to comment.