Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-sizeof

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-sizeof

Get the byte size of an object after JSON.stringify

  • 1.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
655
increased by23.12%
Maintainers
1
Weekly downloads
 
Created
Source

⭕ json-sizeof ⭕

Documentation Documentation Documentation Maintenance License: MIT

Get the byte size of an object after JSON.stringify

📩 Installation

npm i json-sizeof

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));

Keywords

FAQs

Package last updated on 26 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc