# memoizesync

> Helper for memoizing synchronous functions and methods

Latest version **1.1.1** (published 2017-12-27) · BSD license · 0 weekly downloads

## Install

```sh
npm install memoizesync
pnpm add memoizesync
yarn add memoizesync
bun add memoizesync
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2017-12-27 |
| First published | 2012-12-12 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Andreas Lind Petersen |
| Maintainers | papandreou |
| Keywords | memo, memoize, sync, cache |

## Links

- npm: https://www.npmjs.com/package/memoizesync
- Repository: https://github.com/papandreou/node-memoizesync
- Homepage: https://github.com/papandreou/node-memoizesync#readme
- Issues: https://github.com/papandreou/node-memoizesync/issues
- npm.io page: https://npm.io/package/memoizesync

## Dependencies (1)

- [lru-cache](https://npm.io/package/lru-cache.md) =2.3.1

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2017-12-27
- 1.1.0 — 2017-12-27
- 1.0.0 — 2017-02-20
- 0.4.3 — 2017-02-20
- 0.5.0 — 2014-04-26
- 0.4.2 — 2013-10-06
- 0.4.1 — 2013-10-06
- 0.4.0 — 2013-10-06
- 0.3.0 — 2013-10-06
- 0.2.1 — 2013-09-06
- 0.2.0 — 2013-08-27
- 0.1.0 — 2013-04-03
- 0.0.1 — 2012-12-12

## README

node-memoizesync
================

Yet another memoizer for synchronous functions.

[![NPM version](https://badge.fury.io/js/memoizesync.png)](http://badge.fury.io/js/memoizesync)
[![Build Status](https://travis-ci.org/papandreou/node-memoizesync.svg?branch=master)](https://travis-ci.org/papandreou/node-memoizesync)
[![Coverage Status](https://coveralls.io/repos/papandreou/node-memoizesync/badge.png)](https://coveralls.io/r/papandreou/node-memoizesync)
[![Dependency Status](https://david-dm.org/papandreou/node-memoizesync.png)](https://david-dm.org/papandreou/node-memoizesync)

```javascript
var memoizeSync = require('memoizesync');

function myExpensiveComputation(arg1, arg2) {
    // ...
    return result;
}

var memoized = memoizeSync(myExpensiveComputation, options);
```

Now `memoized` works exactly like `myExpensiveComputation`, except that
the actual computation is only performed once for each unique set of
arguments:

```javascript
var result = memoized(42, 100);
// Got the result!

var result2 = memoized(42, 100);
// Got the same result, and much faster this time!
```

The function returned by `memoizeSync` invokes the wrapped function
in the context it's called in itself, so `memoizeSync` even works for
memoizing a method that has access to instance variables:

```javascript
function Foo(name) {
    this.name = name;

    this.myMethod = memoizeSync(function (arg1, arg2) {
        console.log("Cool, this.name works here!", this.name);
        // ...
        return "That was tough, but I'm done now!";
    });
}
```

(Unfortunately setting `Foo.prototype.myMethod = memoizeSync(...)`
wouldn't work as the memoizer would be shared among all instances of
`Foo`).

To distinguish different invocations (whose results need to be cached
separately) `memoizeSync` relies on a naive stringification of the
arguments, which is looked up in an internally kept hash. If the
function you're memoizing takes non-primitive arguments you might want
to provide a custom `argumentsStringifier` in the options argument to
`memoizeSync`. Otherwise all object arguments will be considered equal
because they stringify to `[object Object]`:

```javascript
var memoized = memoizeSync(function functionToMemoize(obj) {
    // ...
    return Object.keys(obj).join('');
}, {
    argumentsStringifier: function (args) {
        return args.map(function (arg) {return JSON.stringify(arg);}).join(",");
    }
);

memoized({foo: 'bar'}); // 'foo'

memoized({quux: 'baz'}); // 'quux'
```

Had the custom `argumentsStringifier` not been provided, the memoized
function would would have returned `foo` both times.

If the `argumentsStringifier` returns false, the cache will be bypassed.

Check out <a
href="https://github.com/papandreou/node-memoizesync/blob/master/test/memoizeSync.js">the
custom argumentsStringifier test</a> for another example.


### Purging and expiring memoized values ###

You can forcefully clear a specific memoized value using the `purge`
method on the memoizer:

```javascript
var memoized = memoizeSync(function functionToMemoize(foo) {
    // ...
    return theResult;
});
var foo = memoized(123);
memoized.purge(123);
foo = memoized(123); // Will be recomputed
```

`memoizer.purgeAll()` clears all memoized results.

You can also specify a custom ttl (in milliseconds) on the memoized
results:

```javascript
var memoized = memoizeSync(function functionToMemoize() {
    // ...
    return theResult;
}, {maxAge: 1000});
```

In the above example the memoized value will be considered stale one
second after it has been computed, and it will be recomputed next time
`memoizeSync` is invoked with the same arguments.

`memoizeSync` uses <a
href="https://github.com/isaacs/node-lru-cache">node-lru-cache</a> to
store the memoized values, and it accepts the same parameters in the
`options` object.

If you want to use the `length` option for lru-cache, note that the
memoized values are arrays: `[exception, returnValue]`.

```javascript
var memoizedFsReadFileSync = memoizeSync(require('fs').readFileSync, {
    max: 1000000,
    length: function (exceptionAndReturnValue) {
        if (exceptionAndReturnValue[0]) {
            return 1;
        } else {
            var body = exceptionAndReturnValue[1];
            return Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body);
        }
    },
    maxAge: 1000
});
```

The LRU instance is exposed in the `cache` property of the memoized
function in case you need to access it.

Installation
------------

Make sure you have node.js and npm installed, then run:

    npm install memoizesync

Browser compatibility
---------------------

`memoizeSync` uses the UMD wrapper, so it should also work in
browsers. You should also have the <a
href="https://github.com/isaacs/node-lru-cache">node-lru-cache</a>
included:

```html
<script src="lru-cache.js"></script>
<script src="memoizeSync.js"></script>
<script>
    var memoizedFunction = memoizeSync(function () {
        // ...
    });
</script>
```

`lru-cache` uses `Object.defineProperty` and doesn't include an UMD
wrapper, but if you define a `shims` config it should be possible to
get it memoizeSync working with require.js, at least in newer browsers.

License
-------

3-clause BSD license -- see the `LICENSE` file for details.

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