Giter VIP home page Giter VIP logo

json-sizeof's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.