json-sizeof
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

json-sizeof

Documentation Documentation Documentation Maintenance License: MIT

Get the byte size of an object after JSON.stringify

📩 Installation

npm i json-sizeof

🔗 Links

npm package
Github page

📋 Usage

Example

const { jsonSizeOf } = require('json-sizeof');

const obj = {
  str1: 'I am a string!',
  obj1: {
    str2: 456,
    obj2: null,
    obj3: undefined
  },
};

const bytes = jsonSizeOf(obj);
// expected 57

📌 Why

jsonSizeOf(obj) equals to Buffer.byteLength(JSON.stringify(obj)).
but is faster and less likely to cause "Javascript heap out of memory"

const obj = {};
for (let i = 0;i < 8000000;i++) {
  obj['test' + i] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
}

// expected: 342888891
jsonSizeOf(obj);

// The following line will cause "JavaScript heap out of memory" fatal error
// if you do not maually increase the memory usage of node app.
Buffer.byteLength(JSON.stringify(obj));

Package Sidebar

Install

npm i json-sizeof

Weekly Downloads

988

Version

1.5.0

License

ISC

Unpacked Size

11.8 kB

Total Files

6

Last publish

Collaborators

  • a179346