From 18e5d95d6221d19fec989954cdee790a4c4f18bd Mon Sep 17 00:00:00 2001 From: Andrei Karpushonak Date: Sat, 28 Jan 2023 21:49:25 +0100 Subject: [PATCH] Added BigInt support. Closes https://github.com/miktam/sizeof/issues/54 (#75) --- README.md | 5 +++-- indexv2.js | 2 ++ package.json | 2 +- test/test.js | 4 ++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 92e874a..3e2248f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/indexv2.js b/indexv2.js index c358a95..42c68ab 100644 --- a/indexv2.js +++ b/indexv2.js @@ -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) diff --git a/package.json b/package.json index cb6e0c8..e67fe2f 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/test.js b/test/test.js index 541b0eb..890dedc 100644 --- a/test/test.js +++ b/test/test.js @@ -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) + }) })