From 1aded8c40129e44f93ee62bc5c5c3980945f1ad2 Mon Sep 17 00:00:00 2001 From: Andrei Karpushonak Date: Tue, 7 Feb 2023 18:24:26 +0100 Subject: [PATCH 1/3] Calculate string size in a browser precisely. Fixes https://github.com/miktam/sizeof/issues/82 --- indexv2.js | 2 +- package.json | 2 +- test/test.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/indexv2.js b/indexv2.js index ae0716c..bf6aea1 100644 --- a/indexv2.js +++ b/indexv2.js @@ -87,7 +87,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.json b/package.json index 10bb53c..1b20b3d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "object-sizeof", - "version": "2.6.0", + "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..cb1b5e3 100644 --- a/test/test.js +++ b/test/test.js @@ -143,8 +143,8 @@ 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) }) afterEach(function () { From f912efbea67b8d6999827e3a2582ddb3ff5b01d8 Mon Sep 17 00:00:00 2001 From: Andrei Karpushonak Date: Sat, 11 Feb 2023 17:11:41 +0100 Subject: [PATCH 2/3] Use new Buffer.from().byteLength for node. Special case for an empty string. Tested on node v12 - works. Does not work on node v10. Continuation of https://github.com/miktam/sizeof/issues/82 --- indexv2.js | 6 ++-- package-lock.json | 90 ++--------------------------------------------- test/test.js | 15 ++++++-- 3 files changed, 19 insertions(+), 92 deletions(-) diff --git a/indexv2.js b/indexv2.js index f8c0504..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 } /** 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/test/test.js b/test/test.js index cb1b5e3..0854ebd 100644 --- a/test/test.js +++ b/test/test.js @@ -3,6 +3,7 @@ /* global describe, it */ const should = require('should') +const v8 = require('v8') const sizeof = require('../indexv2.js') describe('sizeof node.js tests', () => { @@ -23,12 +24,20 @@ 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) + sizeof('').should.be.equal(v8.serialize('').byteLength) + sizeof('').should.be.equal(4) + }) + + it('sizeof of a long string', () => { + sizeof( + '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.' + ).should.be.equal(171) }) it('boolean size shall be 4', () => { From 4d7efa501222ece8b516f02ff18d1d9ef0f0909b Mon Sep 17 00:00:00 2001 From: Andrei Karpushonak Date: Sat, 11 Feb 2023 17:39:30 +0100 Subject: [PATCH 3/3] Add long string for browser test --- test/test.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/test.js b/test/test.js index 0854ebd..c5e0fd8 100644 --- a/test/test.js +++ b/test/test.js @@ -5,6 +5,8 @@ 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', () => { @@ -30,14 +32,13 @@ describe('sizeof node.js tests', () => { }) it('sizeof of empty string', () => { - sizeof('').should.be.equal(v8.serialize('').byteLength) - sizeof('').should.be.equal(4) + const emptyString = '' + sizeof(emptyString).should.be.equal(v8.serialize(emptyString).byteLength) + sizeof(emptyString).should.be.equal(4) }) it('sizeof of a long string', () => { - sizeof( - '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.' - ).should.be.equal(171) + sizeof(LONG_STRING).should.be.equal(171) }) it('boolean size shall be 4', () => { @@ -156,6 +157,10 @@ describe('sizeof browser tests', () => { sizeof('abc').should.be.equal(3) }) + it('sizeof of a long string', () => { + sizeof(LONG_STRING).should.be.equal(171) + }) + afterEach(function () { delete global.window delete global.document