Skip to content

Commit

Permalink
Added BigInt support. Closes #54 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
miktam authored Jan 28, 2023
1 parent 42a0c05 commit 18e5d95
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

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.

### Complex types support
### Standard built in types support

- Map
- Set
- BigInt

### Get size of a JavaScript object in Bytes - Browser

Expand Down Expand Up @@ -47,7 +48,7 @@ Please note, that V8 which compiles the JavaScript into native machine code, is
import sizeof from 'object-sizeof'
// 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"
console.log('Integer 12345 in bytes: ' + sizeof(12345)) // "Integer 12345 in bytes: 8"
```

### Licence
Expand Down
2 changes: 2 additions & 0 deletions indexv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ function objectSizeBrowser (obj) {
} else {
bytes += (obj.toString().length - 8) * ECMA_SIZES.STRING
}
} else if (typeof value === 'bigint') {
bytes += Buffer.from(value.toString()).byteLength
} else if (typeof value === 'object' && objectList.indexOf(value) === -1) {
objectList.push(value)

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.2.0",
"version": "2.3.0",
"description": "Sizeof of a JavaScript object in Bytes",
"main": "indexv2.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ describe('sizeof', () => {
biggerSet.add('some text') // Set(3) { 1, 5, 'some text' }
sizeof(biggerSet).should.be.above(sizeof(smallerSet))
})

it('BigInt support', () => {
sizeof(BigInt(21474836480)).should.equal(11)
})
})

0 comments on commit 18e5d95

Please sign in to comment.