Code Monkey home page Code Monkey logo

sizeof's Introduction

object-sizeof

Build GitHub contributors NPM codecov

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 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, 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

// 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

sizeof's People

Contributors

miktam avatar dependabot[bot] avatar omnijeff avatar pyrolistical avatar carlo-quinonez avatar r3dcrosse avatar x1unix avatar jacenko avatar emohankrishna avatar hadrien-toma avatar hetul avatar jamesacres avatar fazendaaa avatar crorc avatar aw-davidson avatar fossabot avatar

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.