Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nano-memoize

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nano-memoize

Faster than fast, smaller than micro ... a nano speed and nano size memoizer.

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
90K
decreased by-11.1%
Maintainers
1
Weekly downloads
 
Created
Source

Faster than fast, smaller than micro ... nano-memoizer.

Introduction

The devs caiogondim and planttheidea have produced great memoizers. We analyzed their code to see if we could build something faster than fast-memoize and smaller than micro-memoize while adding back some of the functionality of moize removed in micro-memoize. We think we have done it ... but credit to them ... we just merged the best ideas in both and eliminated excess code.

The minified/gzipped size is 872 bytes for nano-memoize vs 959 bytes for micro-memoize. And, nano-memoize has slightly more functionality.

The speed tests are below. nano-memoize is the fastest in all cases. For single argument functions is it comparable to, but slightly and probably un-importantly faster than, fast-memoize. For multiple argument functions it is comparable to, but slightly and probably un-importantly faster than, micro-memoize.

We have found that benchmarks can vary dramatically from O/S to O/S or Node version to Node version. These tests were run on a Windows 10 64bit 2.4ghz machine with 8GB RAM and Node v9.4.0.

Functions with a single primitive parameter...

NameOps / secRelative margin of errorSample size
nano-memoize152,526,010� 2.58%80
fast-memoize147,683,192� 2.90%85
micro-memoize22,682,348� 3.69%75
iMemoized22,292,411� 4.47%72
lodash20,937,311� 1.94%88
moize16,296,876� 4.77%74
memoizee9,651,118� 3.07%86
underscore9,266,277� 2.66%75
lru-memoize6,676,849� 2.93%87
addy-osmani3,899,834� 2.27%86
memoizerific3,753,347� 2.33%86
ramda493,665� 1.77%88

Functions with a single object parameter...

NameOps / secRelative margin of errorSample size
nano-memoize53,741,011� 2.06%85
fast-memoize51,041,370� 2.40%82
micro-memoize22,638,078� 3.96%77
lodash22,187,376� 1.72%83
moize19,446,817� 3.32%81
underscore13,643,959� 3.17%81
iMemoized11,926,976� 5.90%80
memoizee8,010,016� 1.99%83
lru-memoize5,709,156� 1.89%89
memoizerific3,817,781� 1.46%90
addy-osmani3,699,956� 3.30%85
ramda793,756� 1.92%87

Functions with multiple parameters that contain only primitives...

NameOps / secRelative margin of errorSample size
nano-memoize18,408,074� 1.24%85
micro-memoize17,310,593� 1.11%85
moize14,457,697� 0.79%90
memoizee7,723,320� 0.54%94
iMemoized5,934,041� 1.03%90
lru-memoize5,388,273� 0.52%94
memoizerific3,206,479� 0.30%93
addy-osmani2,397,744� 0.48%93
fast-memoize899,483� 0.37%91

Functions with multiple parameters that contain objects...

NameOps / secRelative margin of errorSample size
nano-memoize13,869,690� 1.25%86
micro-memoize13,192,239� 3.13%78
moize10,895,627� 3.33%71
memoizee5,794,981� 0.83%92
lru-memoize5,148,065� 0.44%92
memoizerific3,206,713� 0.86%93
addy-osmani996,705� 0.45%92
fast-memoize699,597� 1.17%91

Deep equals ...

NameOps / secRelative margin of errorSample size
nano-memoize deep equals (lodash isEqual)18,024,422� 1.78%83
micro-memoize deep equals (lodash isEqual)17,219,476� 0.69%86
nano-memoize deep equals (fast-equals deepEqual)14,732,731� 3.10%85
micro-memoize deep equals (fast-equals deepEqual)8,785,408� 11.28%51
micro-memoize deep equals (hash-it isEqual)5,744,080� 10.69%48

We were puzzled about the multiple argument performance on fast-memoize given its stated goal of being the "fastest possible". We discovered that the default caching and serialization approach used by fast-memoize only performs well for single argument functions for two reasons:

  1. It uses JSON.stringify to create a key for an entire argument list. This can be slow.

  2. Because a single key is generated for all arguments when perhaps only the first argument differs in a call, a lot of extra work is done. The moize and micro-memoize approach adopted by nano-memoize is far faster for multiple arguments.

Usage

npm install nano-memoize

use the code in the browser directory for the browser

Since most devs are running a build pipeline, the code is not transpiled, although it is browserified

API

The API is a subset of the moize API.

const memoized = micromemoize(sum(a,b) => a + b);
memoized(1,2); // 3
memoized(1,2); // pulled from cache

memoized(function,options) returns function

The shape of options is:

{
  maxArgs: number, // only use the provided maxArgs for cache look-up, useful for ignoring final callback arguments
  maxAge: number, // number of milliseconds to cache a result
  serializer: function, // the serializer/key generator to use for single argument functions (multi-argument functions do not use a serializer)
  equals: function, // the equals function to use for multi-argument functions, e.g. deepEquals for objects (single-argument functions use serializer not equals)
  vargs: boolean // forces the use of multi-argument paradigm, auto set if function has a spread argument or uses `arguments` in its body.
}

# Release History (reverse chronological order)

2018-02-07 v0.0.2 Documentation updates

2018-02-07 v0.1.1 Documentationand benchmark updates

2018-02-01 v0.1.0 Documentation updates. 50 byte decrease.

2018-01-27 v0.0.7b BETA Documentation updates.

2018-01-27 v0.0.6b BETA Minor size and speed improvements.

2018-01-27 v0.0.5b BETA Fixed edge case where multi-arg key may be shorter than current args.

2018-01-27 v0.0.4b BETA Fixed benchmarks. Removed maxSize. More unit tests. Fixed maxAge.

2018-01-27 v0.0.3b BETA More unit tests. Documentation. Benchmark code in repository not yet running.

2018-01-24 v0.0.2a ALPHA Minor speed enhancements. Benchmark code in repository not yet running.

2018=01-24 v0.0.1a ALPHA First public release. Benchmark code in repository not yet running.

Keywords

FAQs

Package last updated on 07 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc