pax_global_header00006660000000000000000000000064130017327440014513gustar00rootroot0000000000000052 comment=c12270441fab7f42fe53cf97edd53c60c4a8268f mem-1.1.0/000077500000000000000000000000001300173274400122705ustar00rootroot00000000000000mem-1.1.0/.editorconfig000066400000000000000000000002761300173274400147520ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 mem-1.1.0/.gitattributes000066400000000000000000000000351300173274400151610ustar00rootroot00000000000000* text=auto *.js text eol=lf mem-1.1.0/.gitignore000066400000000000000000000000151300173274400142540ustar00rootroot00000000000000node_modules mem-1.1.0/.travis.yml000066400000000000000000000000531300173274400143770ustar00rootroot00000000000000language: node_js node_js: - '6' - '4' mem-1.1.0/index.js000066400000000000000000000020531300173274400137350ustar00rootroot00000000000000'use strict'; const mimicFn = require('mimic-fn'); const cacheStore = new WeakMap(); const defaultCacheKey = function (x) { if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) { return x; } return JSON.stringify(arguments); }; module.exports = (fn, opts) => { opts = Object.assign({ cacheKey: defaultCacheKey, cache: new Map() }, opts); const memoized = function () { const cache = cacheStore.get(memoized); const key = opts.cacheKey.apply(null, arguments); if (cache.has(key)) { const c = cache.get(key); if (typeof opts.maxAge !== 'number' || Date.now() < c.maxAge) { return c.data; } } const ret = fn.apply(null, arguments); cache.set(key, { data: ret, maxAge: Date.now() + (opts.maxAge || 0) }); return ret; }; mimicFn(memoized, fn); cacheStore.set(memoized, opts.cache); return memoized; }; module.exports.clear = fn => { const cache = cacheStore.get(fn); if (cache && typeof cache.clear === 'function') { cache.clear(); } }; mem-1.1.0/license000066400000000000000000000021371300173274400136400ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mem-1.1.0/package.json000066400000000000000000000014771300173274400145670ustar00rootroot00000000000000{ "name": "mem", "version": "1.1.0", "description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", "license": "MIT", "repository": "sindresorhus/mem", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "memoize", "function", "mem", "memoization", "cache", "caching", "optimize", "performance", "ttl", "expire", "promise" ], "dependencies": { "mimic-fn": "^1.0.0" }, "devDependencies": { "ava": "*", "delay": "^1.1.0", "xo": "*" }, "xo": { "esnext": true } } mem-1.1.0/readme.md000066400000000000000000000056271300173274400140610ustar00rootroot00000000000000# mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem) > [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input ## Install ``` $ npm install --save mem ``` ## Usage ```js const mem = require('mem'); let i = 0; const counter = () => ++i; const memoized = mem(counter); memoized('foo'); //=> 1 // cached as it's the same arguments memoized('foo'); //=> 1 // not cached anymore as the arguments changed memoized('bar'); //=> 2 memoized('bar'); //=> 2 ``` ##### Works fine with promise returning functions ```js const mem = require('mem'); let i = 0; const counter = () => Promise.resolve(++i); const memoized = mem(counter); memoized().then(a => { console.log(a); //=> 1 memoized().then(b => { // the return value didn't increase as it's cached console.log(b); //=> 1 }); }); ``` ```js const mem = require('mem'); const got = require('got'); const memGot = mem(got, {maxAge: 1000}); memGot('sindresorhus.com').then(() => { // this call is cached memGot('sindresorhus.com').then(() => { setTimeout(() => { // this call is not cached as the cache has expired memGot('sindresorhus.com').then(() => {}); }, 2000); }); }); ``` ## API ### mem(fn, [options]) #### fn Type: `Function` Function to be memoized. #### options ##### maxAge Type: `number`
Default: `Infinity` Milliseconds until the cache expires. ##### cacheKey Type: `Function` Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array. You could for example change it to only cache on the first argument `x => JSON.stringify(x)`. ##### cache Type: `Object`
Default: `new Map()` Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, and optionally `.clear()`. You could for example use a `WeakMap` instead. ### mem.clear(fn) Clear all cached data of a memoized function. #### fn Type: `Function` Memoized function. ## Tips ### Cache statistics If you want to know how many times your cache had a hit or a miss, you can make use of [stats-map](https://github.com/SamVerschueren/stats-map) as a replacement for the default cache. #### Example ```js const mem = require('mem'); const StatsMap = require('stats-map'); const got = require('got'); const cache = new StatsMap(); const memGot = mem(got, {cache}); memGot('sindresorhus.com') .then(() => memGot('sindresorhus.com')) .then(() => memGot('sindresorhus.com')); console.log(cache.stats); //=> {hits: 2, misses: 1} ``` ## License MIT © [Sindre Sorhus](https://sindresorhus.com) mem-1.1.0/test.js000066400000000000000000000040201300173274400136010ustar00rootroot00000000000000import test from 'ava'; import delay from 'delay'; import m from './'; test('memoize', t => { let i = 0; const f = () => i++; const memoized = m(f); t.is(memoized(), 0); t.is(memoized(), 0); t.is(memoized(), 0); t.is(memoized('foo'), 1); t.is(memoized('foo'), 1); t.is(memoized('foo'), 1); t.is(memoized('foo', 'bar'), 2); t.is(memoized('foo', 'bar'), 2); t.is(memoized('foo', 'bar'), 2); }); test('memoize with multiple non-primitive arguments', t => { let i = 0; const memoized = m(() => i++); t.is(memoized(), 0); t.is(memoized(), 0); t.is(memoized({foo: true}, {bar: false}), 1); t.is(memoized({foo: true}, {bar: false}), 1); t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2); t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2); }); test('maxAge option', async t => { let i = 0; const f = () => i++; const memoized = m(f, {maxAge: 100}); t.is(memoized(1), 0); t.is(memoized(1), 0); await delay(50); t.is(memoized(1), 0); await delay(200); t.is(memoized(1), 1); }); test('cacheKey option', t => { let i = 0; const f = () => i++; const memoized = m(f, {cacheKey: x => x}); t.is(memoized(1), 0); t.is(memoized(1), 0); t.is(memoized(1, 2), 0); t.is(memoized(2), 1); t.is(memoized(2, 1), 1); }); test('cache option', t => { let i = 0; const f = () => i++; const memoized = m(f, { cache: new WeakMap(), cacheKey: x => x }); const foo = {}; const bar = {}; t.is(memoized(foo), 0); t.is(memoized(foo), 0); t.is(memoized(bar), 1); t.is(memoized(bar), 1); }); test('promise support', async t => { let i = 0; const memoized = m(() => Promise.resolve(i++)); t.is(await memoized(), 0); t.is(await memoized(), 0); t.is(await memoized(10), 1); }); test('preserves the original function name', t => { t.is(m(function foo() {}).name, 'foo'); // eslint-disable-line func-names, prefer-arrow-callback }); test('.clear()', t => { let i = 0; const f = () => i++; const memoized = m(f); t.is(memoized(), 0); t.is(memoized(), 0); m.clear(memoized); t.is(memoized(), 1); t.is(memoized(), 1); });