# object-sizeof

> Sizeof of a JavaScript object in Bytes

Latest version **2.6.5** (published 2024-07-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-sizeof
pnpm add object-sizeof
yarn add object-sizeof
bun add object-sizeof
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.6.5 |
| Published | 2024-07-12 |
| First published | 2014-05-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 17.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 322 |
| Author | Andrei Karpushonak |
| Maintainers | miktam |
| Keywords | sizeof, size, object, bytes |

## Links

- npm: https://www.npmjs.com/package/object-sizeof
- Repository: https://github.com/miktam/sizeof
- Homepage: https://github.com/miktam/sizeof#readme
- Issues: https://github.com/miktam/sizeof/issues
- npm.io page: https://npm.io/package/object-sizeof

## Dependencies (1)

- [buffer](https://npm.io/package/buffer.md) ^6.0.3

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 2.6.5 (latest) — 2024-07-12
- 2.6.4 — 2024-01-19
- 2.6.3 — 2023-07-08
- 2.6.2 — 2023-07-03
- 2.6.1 — 2023-02-11
- 2.6.0 — 2023-02-06
- 2.5.0 — 2023-02-02
- 2.4.2 — 2023-02-01
- 2.4.1 — 2023-01-31
- 2.4.0 — 2023-01-29
- 2.3.0 — 2023-01-28
- 2.2.0 — 2023-01-28
- 2.1.0 — 2023-01-28
- 2.0.0 — 2023-01-24
- 1.6.3 — 2022-02-15
- … 27 more at https://npm.io/package/object-sizeof/versions

## README

## object-sizeof

[![Build](https://img.shields.io/npm/v/object-sizeof)](https://img.shields.io/npm/v/object-sizeof) ![GitHub contributors](https://img.shields.io/github/contributors/miktam/sizeof) [![NPM](https://img.shields.io/npm/dy/object-sizeof)](https://img.shields.io/npm/dy/object-sizeof) [![codecov](https://codecov.io/gh/miktam/sizeof/branch/master/graph/badge.svg?token=qPHxmWpC1K)](https://codecov.io/gh/miktam/sizeof)

### Get the size of a JavaScript object in Bytes

Node.js version uses the Buffer.from(objectToString) method to convert the object's string representation to a buffer, and then it uses the byteLength property to obtain the buffer size in bytes.

The module uses a combination of recursion and a stack to iterate through all of its properties, adding up the number of bytes for each data type it encounters.

Please note that this function will only work in some cases, especially when dealing with complex data structures or when the object contains functions.

### Supported Standard built-in and complex types

- Map
- Set
- BigInt
- Function
- Typed Arrays (Int8Array, Uint32Array, Float64Array, etc)

### Error handling

Errors indicated by returned -1 in following cases:

- JSON serialization error, e.g. circular references.
- Unrecognizable TypedArray object.

It prevents potential exceptions or infinite loops, improving reliability.

### Coding standards

The project follows [JavaScript Standard Style](https://standardjs.com/) as a JavaScript style guide.
Code coverage reports are done using Codecov.io.

Code is written with the assumptions that any code added, which is not tested properly, is already or will be buggy.
Hence test coverage, with the BDD style unit tests, stating the intent, and expected behaviour, is a must.

### Get size of a JavaScript object in Bytes - version 1.x

JavaScript does not provide sizeof (like in C), and programmer does not need to care about memory allocation/deallocation.

However, according to [ECMAScript Language Specification](http://www.ecma-international.org/ecma-262/5.1/), each String value is represented by 16-bit unsigned integer, Number uses the double-precision 64-bit format IEEE 754 values including the special "Not-a-Number" (NaN) values, positive infinity, and negative infinity.

Having this knowledge, the module calculates how much memory object will allocate.

### Installation

`npm install object-sizeof`

### Examples

```javascript
// import sizeof from 'object-sizeof'
const sizeof = require('object-sizeof')
const sizeObj = sizeof({ abc: 'def' })
console.log(`Size of the object: ${sizeObj} bytes`)
const sizeInt = sizeof(12345)
console.log(`Size of the int: ${sizeInt} bytes`)
```

### Licence

The MIT License (MIT)

Copyright (c) 2015, Andrei Karpushonak aka @miktam

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmiktam%2Fsizeof?ref=badge_shield)

---
_Source: https://npm.io/package/object-sizeof · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
