diff --git a/indexv2.js b/indexv2.js index eae5ba1..5005458 100644 --- a/indexv2.js +++ b/indexv2.js @@ -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 } /** @@ -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 diff --git a/package-lock.json b/package-lock.json index 681b359..e3dc1eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "object-sizeof", - "version": "2.4.1", + "version": "2.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "object-sizeof", - "version": "2.4.1", + "version": "2.7.0", "license": "MIT", "dependencies": { "buffer": "^6.0.3" @@ -15,7 +15,6 @@ "codecov": "^3.8.3", "mocha": "^10.2.0", "nyc": "^15.1.0", - "proxyquire": "^2.1.3", "should": "^13.2.3", "standard": "^17.0.0" } @@ -849,6 +848,7 @@ "version": "0.0.2", "resolved": "https://registry.npmjs.org/argv/-/argv-0.0.2.tgz", "integrity": "sha512-dEamhpPEwRUBpLNHeuCm/v+g0anFByHahxodVO/BbAarHVBBg2MccCwf9K+o1Pof+2btdnkJelYVUWjW/VrATw==", + "deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.", "dev": true, "engines": { "node": ">=0.6.10" @@ -2082,19 +2082,6 @@ "node": "^10.12.0 || >=12.0.0" } }, - "node_modules/fill-keys": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/fill-keys/-/fill-keys-1.0.2.tgz", - "integrity": "sha512-tcgI872xXjwFF4xgQmLxi76GnwJG3g/3isB1l4/G5Z4zrbddGpBjqZCO9oEAcB5wX0Hj/5iQB3toxfO7in1hHA==", - "dev": true, - "dependencies": { - "is-object": "~1.0.1", - "merge-descriptors": "~1.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -2832,15 +2819,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-object": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-object/-/is-object-1.0.2.tgz", - "integrity": "sha512-2rRIahhZr2UWb45fIOuvZGpFtz0TyOZLf32KxBbSoUCeZR495zCKlWUKKUByk3geS2eAs7ZAABt0Y/Rx0GiQGA==", - "dev": true, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/is-path-inside": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz", @@ -3302,12 +3280,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/merge-descriptors": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", - "integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==", - "dev": true - }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -3390,12 +3362,6 @@ "node": ">=10" } }, - "node_modules/module-not-found-error": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/module-not-found-error/-/module-not-found-error-1.0.1.tgz", - "integrity": "sha512-pEk4ECWQXV6z2zjhRZUongnLJNUeGQJ3w6OQ5ctGwD+i5o93qjRQUk2Rt6VdNeu3sEP0AB4LcfvdebpxBRVr4g==", - "dev": true - }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -4106,17 +4072,6 @@ "react-is": "^16.13.1" } }, - "node_modules/proxyquire": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/proxyquire/-/proxyquire-2.1.3.tgz", - "integrity": "sha512-BQWfCqYM+QINd+yawJz23tbBM40VIGXOdDw3X344KcclI/gtBbdWF6SlQ4nK/bYhF9d27KYug9WzljHC6B9Ysg==", - "dev": true, - "dependencies": { - "fill-keys": "^1.0.2", - "module-not-found-error": "^1.0.1", - "resolve": "^1.11.1" - } - }, "node_modules/punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", @@ -6608,16 +6563,6 @@ "flat-cache": "^3.0.4" } }, - "fill-keys": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/fill-keys/-/fill-keys-1.0.2.tgz", - "integrity": "sha512-tcgI872xXjwFF4xgQmLxi76GnwJG3g/3isB1l4/G5Z4zrbddGpBjqZCO9oEAcB5wX0Hj/5iQB3toxfO7in1hHA==", - "dev": true, - "requires": { - "is-object": "~1.0.1", - "merge-descriptors": "~1.0.0" - } - }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -7121,12 +7066,6 @@ "has-tostringtag": "^1.0.0" } }, - "is-object": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/is-object/-/is-object-1.0.2.tgz", - "integrity": "sha512-2rRIahhZr2UWb45fIOuvZGpFtz0TyOZLf32KxBbSoUCeZR495zCKlWUKKUByk3geS2eAs7ZAABt0Y/Rx0GiQGA==", - "dev": true - }, "is-path-inside": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz", @@ -7465,12 +7404,6 @@ "semver": "^6.0.0" } }, - "merge-descriptors": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", - "integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==", - "dev": true - }, "minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -7535,12 +7468,6 @@ } } }, - "module-not-found-error": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/module-not-found-error/-/module-not-found-error-1.0.1.tgz", - "integrity": "sha512-pEk4ECWQXV6z2zjhRZUongnLJNUeGQJ3w6OQ5ctGwD+i5o93qjRQUk2Rt6VdNeu3sEP0AB4LcfvdebpxBRVr4g==", - "dev": true - }, "ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -8069,17 +7996,6 @@ "react-is": "^16.13.1" } }, - "proxyquire": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/proxyquire/-/proxyquire-2.1.3.tgz", - "integrity": "sha512-BQWfCqYM+QINd+yawJz23tbBM40VIGXOdDw3X344KcclI/gtBbdWF6SlQ4nK/bYhF9d27KYug9WzljHC6B9Ysg==", - "dev": true, - "requires": { - "fill-keys": "^1.0.2", - "module-not-found-error": "^1.0.1", - "resolve": "^1.11.1" - } - }, "punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", diff --git a/package.json b/package.json index c599a0b..1b20b3d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/test.js b/test/test.js index 2553033..c5e0fd8 100644 --- a/test/test.js +++ b/test/test.js @@ -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', () => { @@ -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', () => { @@ -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 () {