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

memily

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memily

Javascript function memoization library.

  • 1.1.15
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Memily CircleCI npm

Javascript memoization library. Wrap your computationally expensive functions with memily so that the results of calls are cached.

Browser Compatibility

This package is dependendent on the ES6 Map feature, which is available in most modern browsers. Consider using a polyfill such as core-js for legacy browser support.

Installation

Install memily to your project by running:

npm install --save memily

Or if you use Yarn:

yarn add memily

Then, import memily into your project like this:

import memily from 'memily';

Examples

Basic Usage
import memily from 'memily';

function squareRoot(num) {
    console.log('Hello world!');
    return Math.sqrt(num);
}

const squareRootMemoized = memily(squareRoot);

squareRootMemoized(4); // console.log: 'Hello world!'
squareRootMemoized(4); // ...
squareRootMemoized(4); // ...
squareRootMemoized(9); // console.log: 'Hello world!'
Caching for a finite time using maxAge option.
function squareRoot(num) {
    console.log('Hello world!');
    return Math.sqrt(num);
}

const squareRootMemoized = memily(squareRoot, { maxAge: 100 });

Promise.resolve()
    .then(() => squareRootMemoized(4)) // console.log: 'Hello world!'
    .then(() => squareRootMemoized(4)) // ...
    .then(() => new Promise(resolve => setTimeout(resolve, 200)))
    .then(() => squareRootMemoized(4)); // console.log: 'Hello world!'
Caching against a custom key using the cacheKey option.
const steveHolt = {
    id: 1,
    firstName: 'Steve',
    surname: 'Holt',
};

const tobiasFunke = {
    id: 2,
    firstName: 'Tobias',
    surname: 'Funke',
};

function getFullNameString(user) {
    console.log('Hello world!');
    return `${user.firstName} ${user.surname}`;
}

const getFullNameStringMemoized = memily(getFullNameString, {
    cacheKey: user => user.id,
});

getFullNameStringMemoized(steveHolt); // console.log: 'Hello world!'
getFullNameStringMemoized(steveHolt); // ...
getFullNameStringMemoized(tobiasFunke); // console.log: 'Hello world!'
getFullNameStringMemoized(tobiasFunke); // ...
Flushing the memoization cache
import { flush } from 'memily';

flush();

Flow Types

Memily comes with Flow types built in. No flow-typed installation required.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgements

Memily was heavily inspired and influenced by sindresorhus's mem package.

Keywords

FAQs

Package last updated on 05 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