# memory-diff

> an object that records memory usage of a code block

Latest version **0.1.1** (published 2018-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install memory-diff
pnpm add memory-diff
yarn add memory-diff
bun add memory-diff
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2018-10-22 |
| First published | 2018-10-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Eyas Ranjous |
| Maintainers | eyas-ranjous |
| Keywords | memory, usage |

## Links

- npm: https://www.npmjs.com/package/memory-diff
- Repository: https://github.com/js-shelf/memory-diff
- Homepage: https://github.com/js-shelf/memory-diff#readme
- Issues: https://github.com/js-shelf/memory-diff/issues
- npm.io page: https://npm.io/package/memory-diff

## Recent versions

- 0.1.1 (latest) — 2018-10-22
- 0.1.0 — 2018-10-13
- 0.0.0 — 2018-10-12

## README

# memory-diff

[![build:?](https://travis-ci.org/js-shelf/memory-diff.svg?branch=master)](https://travis-ci.org/js-shelf/memory-diff) [![npm](https://img.shields.io/npm/v/memory-diff.svg)](https://www.npmjs.com/package/memory-diff) [![npm](https://img.shields.io/npm/dm/memory-diff.svg)](https://www.npmjs.com/package/memory-diff) [![npm](https://img.shields.io/badge/node-%3E=%206.0-blue.svg)](https://www.npmjs.com/package/memory-diff)

A simple object that enables recording heap memory that is consumed by some function/code and format the result.

## Install
```
npm install --save memory-diff
```

## Usage

### Creation
```js
const memoryDiffFn = require('memory-diff');
const memoryDiff = memoryDiffFn('test-function');
```

### .start()
starts recording heap memory size.

```js
memoryDiff.start();
```

### .stop()
stops recording heap memory size.

```js
memoryDiff.stop();
```

### .megabytes()
return the MB part in the recorded memory

```js
console.log(memoryDiff.megabytes()); // 3
```

### .kilobytes()
return the KB part in the recorded memory

```js
console.log(memoryDiff.kilobytes()); // 7
```

### .bytes()
return the Bytes part in the recorded memory

```js
console.log(memoryDiff.bytes()); // 588
```

### .isRunning()
checks if the memoryDiff is running and hasn't been stopped

```js
console.log(memoryDiff.isRunning()); // false
```

### .isStopped()
checks if the memoryDiff has been stopped

```js
console.log(memoryDiff.isStopped()); // true
```

### .format(template)
formats the recorded memory using a custom or default template. The function replaces the memory size fractions placeholders in a string. Placeholders are:

* `%label` for the memoryDiff label.
* `%mb` for Megabytes.
* `%kb` for Kilobytes.
* `%b` for Bytes.

```js
// using the default template
console.log(memoryDiff.format()); // memory-test: 3 MB, 7 KB, 588 bytes

// using a custom template
const custom = '%label [%mb MB - %kb KB - %b B ]';
console.log(memoryDiff.format(custom)); // memory-test [3 MB - 7 KB - 588 B ]
```

### .clear()
clears the memoryDiff values. Can be started again to record new values.

```js
memoryDiff.clear();
console.log(memoryDiff.megabytes()); // null
```

### Example
```js
const test = () => {
  memoryDiff.start();
  const arr = Array(1000000).fill('test');
  memoryDiff.stop();
};
test();
console.log(memoryDiff.format()); // 'test: 8 MB, 115 KB, 56 bytes'
```

## Build
```
grunt build
```

## License
The MIT License. Full License is [here](https://github.com/js-shelf/memory-diff/blob/master/LICENSE)

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