Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions indexv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const Buffer = require('buffer/').Buffer

/**
* Precisely calculate size of string in node
* Based on https://stackoverflow.com/questions/68789144/how-much-memory-do-v8-take-to-store-a-string/68791382#68791382
* @param {} str
*/
function preciseStringSizeNode (str) {
return 12 + 4 * Math.ceil(str.length / 4)
if (str === '') {
return 4
}
return new Buffer.from(str).byteLength
}

/**
Expand Down Expand Up @@ -88,7 +90,7 @@ function objectSizeSimple (obj) {
if (isNodeEnvironment()) {
bytes += preciseStringSizeNode(value)
} else {
bytes += value.length * ECMA_SIZES.STRING
bytes += new TextEncoder().encode(value).length
}
} else if (typeof value === 'number') {
bytes += ECMA_SIZES.NUMBER
Expand Down
90 changes: 3 additions & 87 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.6.1",
"version": "2.7.0",
"description": "Sizeof of a JavaScript object in Bytes",
"main": "indexv2.js",
"scripts": {
Expand Down
24 changes: 19 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
/* global describe, it */

const should = require('should')
const v8 = require('v8')
const sizeof = require('../indexv2.js')
const LONG_STRING =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac vestibulum lacus, sit amet maximus libero. Aliquam erat volutpat. Quisque at orci tortor. Donec at mi nunc.'

describe('sizeof node.js tests', () => {
it('should handle null in object keys', () => {
Expand All @@ -23,12 +26,19 @@ describe('sizeof node.js tests', () => {
sizeof().should.be.equal(0)
})

it('of 3 chars string is 16 in node.js', () => {
sizeof('abc').should.be.equal(16)
it('of 3 chars string is 3 bytes in node.js', () => {
const abcString = 'abc'
sizeof(abcString).should.be.equal(3)
})

it('sizeof of empty string', () => {
sizeof('').should.be.equal(12)
const emptyString = ''
sizeof(emptyString).should.be.equal(v8.serialize(emptyString).byteLength)
sizeof(emptyString).should.be.equal(4)
})

it('sizeof of a long string', () => {
sizeof(LONG_STRING).should.be.equal(171)
})

it('boolean size shall be 4', () => {
Expand Down Expand Up @@ -143,8 +153,12 @@ describe('sizeof browser tests', () => {
global.document = {}
})

it('each caracter is two bytes in a browser environent', () => {
sizeof('abc').should.be.equal(6)
it('in a browser environent string', () => {
sizeof('abc').should.be.equal(3)
})

it('sizeof of a long string', () => {
sizeof(LONG_STRING).should.be.equal(171)
})

afterEach(function () {
Expand Down