Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/a179346/json-sizeof
Get the byte size of an object after JSON.stringify
https://github.com/a179346/json-sizeof
bytes javascript json json-byte json-size json-sizeof nodejs npm npm-package object object-byte object-size size sizeof typescript
Last synced: 12 days ago
JSON representation
Get the byte size of an object after JSON.stringify
- Host: GitHub
- URL: https://github.com/a179346/json-sizeof
- Owner: a179346
- License: mit
- Created: 2020-10-23T08:54:28.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-27T09:05:11.000Z (almost 2 years ago)
- Last Synced: 2024-10-05T01:35:11.664Z (about 1 month ago)
- Topics: bytes, javascript, json, json-byte, json-size, json-sizeof, nodejs, npm, npm-package, object, object-byte, object-size, size, sizeof, typescript
- Language: TypeScript
- Homepage:
- Size: 157 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> Get the byte size of an object after JSON.stringify
## 📩 Installation
```
npm i json-sizeof
```## 🔗 Links
[npm package](https://www.npmjs.com/package/json-sizeof)
[Github page](https://github.com/a179346/json-sizeof)## 📋 Usage
#### Example
```js
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"**
```js
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));
```