{ "_args": [ [ { "raw": "mem@^1.1.0", "scope": null, "escapedName": "mem", "name": "mem", "rawSpec": "^1.1.0", "spec": ">=1.1.0 <2.0.0", "type": "range" }, "/home/jdaugherty/work/GT2/GT2-Android/node_modules/os-locale" ] ], "_from": "mem@>=1.1.0 <2.0.0", "_id": "mem@1.1.0", "_inCache": true, "_location": "/mem", "_nodeVersion": "4.6.0", "_npmOperationalInternal": { "host": "packages-12-west.internal.npmjs.com", "tmp": "tmp/mem-1.1.0.tgz_1476900325889_0.8028518599458039" }, "_npmUser": { "name": "sindresorhus", "email": "sindresorhus@gmail.com" }, "_npmVersion": "2.15.9", "_phantomChildren": {}, "_requested": { "raw": "mem@^1.1.0", "scope": null, "escapedName": "mem", "name": "mem", "rawSpec": "^1.1.0", "spec": ">=1.1.0 <2.0.0", "type": "range" }, "_requiredBy": [ "/os-locale" ], "_resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz", "_shasum": "5edd52b485ca1d900fe64895505399a0dfa45f76", "_shrinkwrap": null, "_spec": "mem@^1.1.0", "_where": "/home/jdaugherty/work/GT2/GT2-Android/node_modules/os-locale", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "bugs": { "url": "https://github.com/sindresorhus/mem/issues" }, "dependencies": { "mimic-fn": "^1.0.0" }, "description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", "devDependencies": { "ava": "*", "delay": "^1.1.0", "xo": "*" }, "directories": {}, "dist": { "shasum": "5edd52b485ca1d900fe64895505399a0dfa45f76", "tarball": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz" }, "engines": { "node": ">=4" }, "files": [ "index.js" ], "gitHead": "c12270441fab7f42fe53cf97edd53c60c4a8268f", "homepage": "https://github.com/sindresorhus/mem#readme", "keywords": [ "memoize", "function", "mem", "memoization", "cache", "caching", "optimize", "performance", "ttl", "expire", "promise" ], "license": "MIT", "maintainers": [ { "name": "sindresorhus", "email": "sindresorhus@gmail.com" } ], "name": "mem", "optionalDependencies": {}, "readme": "# mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem)\n\n> [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\n\n\n## Install\n\n```\n$ npm install --save mem\n```\n\n\n## Usage\n\n```js\nconst mem = require('mem');\n\nlet i = 0;\nconst counter = () => ++i;\nconst memoized = mem(counter);\n\nmemoized('foo');\n//=> 1\n\n// cached as it's the same arguments\nmemoized('foo');\n//=> 1\n\n// not cached anymore as the arguments changed\nmemoized('bar');\n//=> 2\n\nmemoized('bar');\n//=> 2\n```\n\n##### Works fine with promise returning functions\n\n```js\nconst mem = require('mem');\n\nlet i = 0;\nconst counter = () => Promise.resolve(++i);\nconst memoized = mem(counter);\n\nmemoized().then(a => {\n\tconsole.log(a);\n\t//=> 1\n\n\tmemoized().then(b => {\n\t\t// the return value didn't increase as it's cached\n\t\tconsole.log(b);\n\t\t//=> 1\n\t});\n});\n```\n\n```js\nconst mem = require('mem');\nconst got = require('got');\nconst memGot = mem(got, {maxAge: 1000});\n\nmemGot('sindresorhus.com').then(() => {\n\t// this call is cached\n\tmemGot('sindresorhus.com').then(() => {\n\t\tsetTimeout(() => {\n\t\t\t// this call is not cached as the cache has expired\n\t\t\tmemGot('sindresorhus.com').then(() => {});\n\t\t}, 2000);\n\t});\n});\n```\n\n\n## API\n\n### mem(fn, [options])\n\n#### fn\n\nType: `Function`\n\nFunction to be memoized.\n\n#### options\n\n##### maxAge\n\nType: `number`
\nDefault: `Infinity`\n\nMilliseconds until the cache expires.\n\n##### cacheKey\n\nType: `Function`\n\nDetermines 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.\n\nYou could for example change it to only cache on the first argument `x => JSON.stringify(x)`.\n\n##### cache\n\nType: `Object`
\nDefault: `new Map()`\n\nUse 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.\n\n### mem.clear(fn)\n\nClear all cached data of a memoized function.\n\n#### fn\n\nType: `Function`\n\nMemoized function.\n\n\n## Tips\n\n### Cache statistics\n\nIf 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.\n\n#### Example\n\n```js\nconst mem = require('mem');\nconst StatsMap = require('stats-map');\nconst got = require('got');\n\nconst cache = new StatsMap();\nconst memGot = mem(got, {cache});\n\nmemGot('sindresorhus.com')\n\t.then(() => memGot('sindresorhus.com'))\n\t.then(() => memGot('sindresorhus.com'));\n\nconsole.log(cache.stats);\n//=> {hits: 2, misses: 1}\n```\n\n\n## License\n\nMIT © [Sindre Sorhus](https://sindresorhus.com)\n", "readmeFilename": "readme.md", "repository": { "type": "git", "url": "git+https://github.com/sindresorhus/mem.git" }, "scripts": { "test": "xo && ava" }, "version": "1.1.0", "xo": { "esnext": true } }