{"name":"memoize-one","version":"5.1.0-beta.2","description":"A memoization library which only remembers the latest invocation","main":"dist/memoize-one.cjs.js","types":"dist/memoize-one.d.ts","module":"dist/memoize-one.esm.js","sideEffects":false,"author":{"name":"Alex Reardon","email":"alexreardon@gmail.com"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/alexreardon/memoize-one.git"},"keywords":["memoize","memoization","cache","performance"],"dependencies":{},"devDependencies":{"@types/jest":"^24.0.18","@types/lodash.isequal":"^4.5.5","@typescript-eslint/eslint-plugin":"^2.0.0","@typescript-eslint/parser":"^2.0.0","benchmark":"^2.1.4","cross-env":"^5.2.0","eslint":"6.2.0","eslint-config-prettier":"^6.1.0","eslint-plugin-jest":"^22.15.1","eslint-plugin-prettier":"^3.1.0","flow-bin":"^0.105.2","jest":"^24.9.0","lodash.isequal":"^4.5.0","prettier":"1.18.2","rimraf":"3.0.0","rollup":"^1.19.4","rollup-plugin-commonjs":"^10.0.2","rollup-plugin-replace":"^2.2.0","rollup-plugin-typescript":"^1.0.1","rollup-plugin-uglify":"^6.0.2","ts-jest":"^24.0.2","ts-node":"^8.3.0","tslib":"^1.10.0","typescript":"^3.5.3"},"config":{"prettier_target":"src/**/*.{ts,js,jsx,md,json} test/**/*.{ts,js,jsx,md,json}"},"scripts":{"validate":"yarn lint && yarn typecheck","test":"yarn jest","typecheck":"yarn tsc --noEmit","prettier:check":"yarn prettier --debug-check $npm_package_config_prettier_target","prettier:write":"yarn prettier --write $npm_package_config_prettier_target","lint:eslint":"eslint $npm_package_config_prettier_target","lint":"yarn lint:eslint && yarn prettier:check","build":"yarn build:clean && yarn build:dist && yarn build:typescript && yarn build:flow","build:clean":"rimraf dist","build:dist":"rollup -c","build:typescript":"tsc ./src/memoize-one.ts --emitDeclarationOnly --declaration --outDir ./dist","build:flow":"cp src/memoize-one.js.flow dist/memoize-one.cjs.js.flow","perf":"ts-node ./benchmarks/shallow-equal.ts","prepublishOnly":"yarn build"},"readme":"# memoize-one\n\nA memoization library that only caches the result of the most recent arguments.\n\n[![Build Status](https://travis-ci.org/alexreardon/memoize-one.svg?branch=master)](https://travis-ci.org/alexreardon/memoize-one)\n[![npm](https://img.shields.io/npm/v/memoize-one.svg)](https://www.npmjs.com/package/memoize-one)\n[![dependencies](https://david-dm.org/alexreardon/memoize-one.svg)](https://david-dm.org/alexreardon/memoize-one)\n[![Downloads per month](https://img.shields.io/npm/dm/memoize-one.svg)](https://www.npmjs.com/package/memoize-one)\n[![min](https://img.shields.io/bundlephobia/min/memoize-one.svg)](https://www.npmjs.com/package/memoize-one)\n[![minzip](https://img.shields.io/bundlephobia/minzip/memoize-one.svg)](https://www.npmjs.com/package/memoize-one)\n\n## Rationale\n\nUnlike other memoization libraries, `memoize-one` only remembers the latest arguments and result. No need to worry about cache busting mechanisms such as `maxAge`, `maxSize`, `exclusions` and so on which can be prone to memory leaks. `memoize-one` simply remembers the last arguments, and if the function is next called with the same arguments then it returns the previous result.\n\n## Usage\n\n### Standard usage\n\n```js\nimport memoizeOne from 'memoize-one';\n\nconst add = (a, b) => a + b;\nconst memoizedAdd = memoizeOne(add);\n\nmemoizedAdd(1, 2); // 3\n\nmemoizedAdd(1, 2); // 3\n// Add function is not executed: previous result is returned\n\nmemoizedAdd(2, 3); // 5\n// Add function is called to get new value\n\nmemoizedAdd(2, 3); // 5\n// Add function is not executed: previous result is returned\n\nmemoizedAdd(1, 2); // 3\n// Add function is called to get new value.\n// While this was previously cached,\n// it is not the latest so the cached result is lost\n```\n\n## Installation\n\n```bash\n# yarn\nyarn add memoize-one\n\n# npm\nnpm install memoize-one --save\n```\n\n## Module usage\n\n### ES6 module\n\n```js\nimport memoizeOne from 'memoize-one';\n```\n\n### CommonJS\n\nIf you are in a CommonJS environment (eg [Node](https://nodejs.org)), then **you will need to add `.default` to your import**:\n\n```js\nconst memoizeOne = require('memoize-one').default;\n```\n\n## Custom equality function\n\nYou can also pass in a custom function for checking the equality of two sets of arguments\n\n```js\nconst memoized = memoizeOne(fn, isEqual);\ntype EqualityFn = (newArgs: mixed[], oldArgs: mixed[]) => boolean;\n```\n\nAn equality function should return `true` if the arguments are equal. If `true` is returned then the wrapped function will not be called.\n\nThe default equality function is a shallow equal check of all arguments (each argument is compared with `===`). If the `length` of arguments change, then the default equality function makes no shallow equality checks. You are welcome to decide if you want to return `false` if the `length` of the arguments is not equal\n\n```js\nconst simpleIsEqual: EqualityFn = (newArgs: mixed[], lastArgs: mixed[]): boolean =>\n  newArgs.length === lastArgs.length &&\n  newArgs.every((newArg: mixed, index: number): boolean => shallowEqual(newArg, lastArgs[index]));\n```\n\nA custom equality function needs to compare `Arrays`. The `newArgs` array will be a new reference every time so a simple `newArgs === lastArgs` will always return `false`.\n\nEquality functions are not called if the `this` context of the function has changed (see below).\n\nHere is an example that uses a `lodash.isequal` deep equal equality check\n\n> `lodash.isequal` correctly handles deep comparing two arrays\n\n```js\nimport memoizeOne from 'memoize-one';\nimport isDeepEqual from 'lodash.isequal';\n\nconst identity = x => x;\n\nconst shallowMemoized = memoizeOne(identity);\nconst deepMemoized = memoizeOne(identity, isDeepEqual);\n\nconst result1 = shallowMemoized({ foo: 'bar' });\nconst result2 = shallowMemoized({ foo: 'bar' });\n\nresult1 === result2; // false - difference reference\n\nconst result3 = deepMemoized({ foo: 'bar' });\nconst result4 = deepMemoized({ foo: 'bar' });\n\nresult3 === result4; // true - arguments are deep equal\n```\n\n## `this`\n\n### `memoize-one` correctly respects `this` control\n\nThis library takes special care to maintain, and allow control over the the `this` context for **both** the original function being memoized as well as the returned memoized function. Both the original function and the memoized function's `this` context respect [all the `this` controlling techniques](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md):\n\n- new bindings (`new`)\n- explicit binding (`call`, `apply`, `bind`);\n- implicit binding (call site: `obj.foo()`);\n- default binding (`window` or `undefined` in `strict mode`);\n- fat arrow binding (binding to lexical `this`)\n- ignored this (pass `null` as `this` to explicit binding)\n\n### Changes to `this` is considered an argument change\n\nChanges to the running context (`this`) of a function can result in the function returning a different value even though its arguments have stayed the same:\n\n```js\nfunction getA() {\n  return this.a;\n}\n\nconst temp1 = {\n  a: 20,\n};\nconst temp2 = {\n  a: 30,\n};\n\ngetA.call(temp1); // 20\ngetA.call(temp2); // 30\n```\n\nTherefore, in order to prevent against unexpected results, `memoize-one` takes into account the current execution context (`this`) of the memoized function. If `this` is different to the previous invocation then it is considered a change in argument. [further discussion](https://github.com/alexreardon/memoize-one/issues/3).\n\nGenerally this will be of no impact if you are not explicity controlling the `this` context of functions you want to memoize with [explicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#explicit-binding) or [implicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#implicit-binding). `memoize-One` will detect when you are manipulating `this` and will then consider the `this` context as an argument. If `this` changes, it will re-execute the original function even if the arguments have not changed.\n\n## When your result function `throw`s\n\n> There is no caching when your result function throws\n\nIf your result function `throw`s then the memoized function will also throw. The throw will not break the memoized functions existing argument cache. It means the memoized function will pretend like it was never called with arguments that made it `throw`.\n\n```js\nconst canThrow = (name: string) => {\n  console.log('called');\n  if (name === 'throw') {\n    throw new Error(name);\n  }\n  return { name };\n};\n\nconst memoized = memoizeOne(canThrow);\n\nconst value1 = memoized('Alex');\n// console.log => 'called'\nconst value2 = memoized('Alex');\n// result function not called\n\nconsole.log(value1 === value2);\n// console.log => true\n\ntry {\n  memoized('throw');\n  // console.log => 'called'\n} catch (e) {\n  firstError = e;\n}\n\ntry {\n  memoized('throw');\n  // console.log => 'called'\n  // the result function was called again even though it was called twice\n  // with the 'throw' string\n} catch (e) {\n  secondError = e;\n}\n\nconsole.log(firstError !== secondError);\n\nconst value3 = memoized('Alex');\n// result function not called as the original memoization cache has not been busted\nconsole.log(value1 === value3);\n// console.log => true\n```\n\n## Performance :rocket:\n\n### Tiny\n\n`memoize-one` is super lightweight at [![min](https://img.shields.io/bundlephobia/min/memoize-one.svg?label=)](https://www.npmjs.com/package/memoize-one) minified and [![minzip](https://img.shields.io/bundlephobia/minzip/memoize-one.svg?label=)](https://www.npmjs.com/package/memoize-one) gzipped. (`1KB` = `1,024 Bytes`)\n\n### Extremely fast\n\n`memoize-one` performs better or on par with than other popular memoization libraries for the purpose of remembering the latest invocation.\n\n**Results**\n\n- [simple arguments](https://www.measurethat.net/Benchmarks/ShowResult/4452)\n- [complex arguments](https://www.measurethat.net/Benchmarks/ShowResult/4488)\n\nThe comparisons are not exhaustive and are primarily to show that `memoize-one` accomplishes remembering the latest invocation really fast. The benchmarks do not take into account the differences in feature sets, library sizes, parse time, and so on.\n\n## Code health :thumbsup:\n\n- Tested with all built in [JavaScript types](https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch1.md).\n- 100% code coverage\n- [Continuous integration](https://travis-ci.org/alexreardon/memoize-one) to run tests and type checks.\n- [`Flow` types](http://flowtype.org) for safer internal execution and external consumption. Also allows for editor autocompletion.\n- Follows [Semantic versioning (2.0)](http://semver.org/) for safer consumption.\n- No dependencies\n","readmeFilename":"README.md","gitHead":"21e3715ce0c82d60fc5e73cbeb96ee612436a3c7","bugs":{"url":"https://github.com/alexreardon/memoize-one/issues"},"homepage":"https://github.com/alexreardon/memoize-one#readme","_id":"memoize-one@5.1.0-beta.2","_nodeVersion":"10.15.3","_npmVersion":"6.9.0","dist":{"integrity":"sha512-0V4QdFSeQrJb2KbthcbZzTlUVNOl09OPpxFWbxGecz7Px8Ntickeue8I87Sc9nV/LDA3seiXwFNhi2SN9WQXig==","shasum":"bbb4fe7d452708aa54ab04f1e1060a13c523c7d5","tarball":"http://registry-npm.ywwl.com/memoize-one/download/memoize-one-5.1.0-beta.2.tgz","fileCount":13,"unpackedSize":18956,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdW0/UCRA9TVsSAnZWagAAVywP+QClIZEwF6/UEDCQLKSA\n4JD8stRLhuIo+U5h/3dk1MRgUPEUFsl8e0wkgi7LqfmAmnoPVcwHotdYvA2+\naiF6shavSCdNq+A/dcHCHFF8pLH+E/BmtLx+c8Vxb+/XrjBTkhCbB8iD738j\nb+l8zvhWDEi9LFtnLgLJ4hEfu2xIEfJLIbGcq2ZTz+DSeN0wAwMhuNUQbXuZ\nV4ZsIMmyIQfH4vkdaErB+qDpynVflBLF3aVklgWO3oQvFmP5AV7C0wbt7jM8\nkMQCKYX6Mrswqp/nNFAnYRD3YER2LoBUidvE8Foao8L8ho5YVRDaDXs5jMRX\ndC5drFj+fvmjlVM0DWE1W+NTffz10r4fwMosn6gIMtA3LGwjk3RCRE9EHDm9\nWDqeeK1P5655/pylQnprTgxErBC3alxbNGvZmrPG1/o5CV8O7ireG4HCRo+b\nMiry2nHUg80UI4L1LyJ08tQfOw5Jg4+iTJD5QQgdYhReQU/e/NDeO9anO/QZ\nS2MWRXk5X1MDF5NVv45oSueUpmmh2xfiJaRnVMhubO/zWoO4dyAKgGv4LN1A\nKEOJg9su/cKLTGHm1XaRAQecp4QZbDIpg3x6hP69pgsDv2W7V8OOv2QpA/q0\n/DuRphW/pQTqh3VQAyr/qKilTGHF4weZo7v9hmCqWG0Snanb8oiRYR3WJXO9\nuMHZ\r\n=1xbJ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAyotvevId4R+yOyu9WoFEwEOJ4xq2giSfNTszJnQRViAiEAzBz9dlsDiOzbRQxjZ1esJxztnwXF42XRIw86DRoKSh0="}],"size":6458,"noattachment":false,"key":"/memoize-one/-/memoize-one-5.1.0-beta.2.tgz"},"maintainers":[{"name":"alexreardon","email":"alexreardon@gmail.com"}],"_npmUser":{"name":"alexreardon","email":"alexreardon@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/memoize-one_5.1.0-beta.2_1566265299483_0.5750751587036571"},"_hasShrinkwrap":false,"publish_time":1566265299662,"_cnpm_publish_time":1566265299662,"dist-tags":{"beta":"6.0.0-beta.1","latest":"6.0.0"}}