1.0.3 • Published 8 years ago

sizeomatic v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

sizeomatic

A module that provides methods for approximating the amount of memory being used by a JavaScript object. The results should not be assumed to be precisely accurate, but rather estimates based on useful approximations. It also exports two methods for converting between bytes and larger units of digital storage - which are accurate.

$ npm install sizeomatic

Type/Size Approximation

TypeApproximation
boolean4 bytes
number8 bytes
string2 bytes per character

Size Abbreviations

AbbrSize
K/kKilobyte
M/mMegabyte
G/gGigabyte

Usage

var sizeomatic = require ('sizeomatic');

var bytes = sizeomatic.howManyBytes('125K');
/* result : 128000 */

var notbytes = sizeomatic.pretty(bytes + 1500);
/* result : 126.465K */

var obj =
{
	b : true,
	n : 12345,
	s : "Hello!",
	a : [ "something", "wicked", "this", "way", "comes" ],
	o : { "everything" : "is awesome!" }
};

var size = sizeomatic.getSize(obj);
/* result: 130 (bytes) */

var rsize = sizeomatic.getSize(JSON.stringify(obj));
/* result: 228 (bytes) */

Methods

The following methods are exposed.

howManyBytes

Takes a string representation of an amount of digital storage and returns an integer representation of the number of bytes. The case of the units does not matter, and if you accidentally add a b at the end, it will handle that, too.

console.log(sizeomatic.howManyBytes('125'));
/* 125 */

console.log(sizeomatic.howManyBytes('125B'));
/* 125 */

console.log(sizeomatic.howManyBytes('125Kb'));
/* 128000 */

console.log(sizeomatic.howManyBytes('125kb'));
/* 128000 */

console.log(sizeomatic.howManyBytes('125m'));
/* 131072000 */

console.log(sizeomatic.howManyBytes('125G'));
/* 134217728000 */

getSize

Takes a JavaScript object and returns the approximates size in bytes of memory the object is consuming as an integer.

var obj =
{
	b : true,
	n : 12345,
	s : "Hello!",
	a : [ "something", "wicked", "this", "way", "comes" ],
	o : { "everything" : "is awesome!" }
};

var size = sizeomatic.getSize(obj);
/* result: 130 (bytes) */

var rsize = sizeomatic.getSize(JSON.stringify(obj));
/* result: 228 (bytes) */

Note that a serialized representation will of an object will return a larger value than the original object because it is being treated as a string.

pretty

Takes an integer representation of a number of bytes and formats it to an appropriate size (precise up to three decimal places) suffixed with the appropriate size abbreviation.

console.log(sizeomatic.pretty(125));
/* 125B */

console.log(sizeomatic.pretty(128950));
/* 125.928K */

console.log(sizeomatic.pretty(131672000));
/* 125.572M */

console.log(sizeomatic.pretty(139217728000));
/* 129.657G */

Acknowledgments

I used sizeof as the basis for this module, tweaked it, and added what I wanted.

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago