# memoize-weak-decorator

> Garbage-collected memoizer for variadic functions that can be used as a decorator

Latest version **1.0.3** (published 2017-03-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install memoize-weak-decorator
pnpm add memoize-weak-decorator
yarn add memoize-weak-decorator
bun add memoize-weak-decorator
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2017-03-09 |
| First published | 2017-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Andy Dufilie |
| Maintainers | adufilie |
| Keywords | memoize, weak, decorator |

## Links

- npm: https://www.npmjs.com/package/memoize-weak-decorator
- Repository: https://github.com/adufilie/memoize-weak-decorator
- Homepage: https://github.com/adufilie/memoize-weak-decorator#readme
- Issues: https://github.com/adufilie/memoize-weak-decorator/issues
- npm.io page: https://npm.io/package/memoize-weak-decorator

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2017-03-09
- 1.0.2 — 2017-03-08
- 1.0.1 — 2017-03-08

## README

# Usage

TypeScript example:
```
type XObject = {x: number};
type ResultObject = {result: string};

class Foo
{
	// used as a decorator
	@memoize
	bar(x: XObject, y: string, z?:number):ResultObject
	{
		return {
			result: JSON.stringify({x, y, z})
		};
	}

	// used inline
	baz = memoize({
		getAdditionalArgs: () => [this.y],
		function: (x:XObject):ResultObject => this.bar(x, this.y)
	});

	y = 'y';
}
```

Test:
```
function assert(shouldBeTrue:boolean, msg:string)
{
	if (!shouldBeTrue)
		throw new Error('fail: ' + msg);
}

let foo = new Foo();
let foo2 = new Foo();
let x = {x: 123};
let bar = foo.bar(x, 'y');
let baz = foo.baz(x);

assert(bar === foo.bar(x, 'y'), 'memoized bar');
assert(bar !== foo2.bar(x, 'y'), 'memoized per instance');
assert(bar === baz, "foo.y === 'y' when baz was created");
assert(baz === foo.baz(x), 'memoized baz');
foo.y = 'yy';
assert(baz !== foo.baz(x), 'additional args for baz changed');
assert(foo.bar(x, 'y', 3) === foo.bar(x, 'y', 3), 'supports variable number of arguments');
```

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