memoize-one
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -35,2 +35,7 @@ Object.defineProperty(exports, "__esModule", { | ||
defineProperty(result, 'length', resultFn.length); | ||
var name = 'memoized_' + (resultFn.name || 'fn'); | ||
defineProperty(result, 'name', name); | ||
return result; | ||
@@ -41,2 +46,10 @@ }; | ||
return a === b; | ||
}; | ||
var defineProperty = function defineProperty(target, property, value) { | ||
return Object.defineProperty(target, property, { | ||
writable: false, | ||
configurable: true, | ||
value: value | ||
}); | ||
}; |
{ | ||
"name": "memoize-one", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "A memoization library which only remembers the latest invocation", | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
"sideEffects": false, | ||
"author": "Alex Reardon <alexreardon@gmail.com>", | ||
@@ -12,42 +14,45 @@ "license": "MIT", | ||
}, | ||
"files": [ | ||
"/lib", | ||
"/esm", | ||
"/src" | ||
], | ||
"keywords": [ | ||
"memoize", | ||
"memoization", | ||
"cache", | ||
"performance" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel-cli": "6.24.1", | ||
"babel-core": "6.24.1", | ||
"babel-eslint": "7.2.3", | ||
"babel-cli": "6.26.0", | ||
"babel-core": "6.26.0", | ||
"babel-eslint": "8.2.2", | ||
"babel-plugin-transform-flow-strip-types": "6.22.0", | ||
"babel-plugin-transform-object-rest-spread": "6.23.0", | ||
"babel-preset-es2015": "6.24.1", | ||
"chai": "3.5.0", | ||
"codecov": "2.1.0", | ||
"eslint": "3.19.0", | ||
"eslint-plugin-flowtype": "2.32.1", | ||
"flow-bin": "0.46.0", | ||
"flow-copy-source": "1.1.0", | ||
"mocha": "3.3.0", | ||
"nyc": "10.3.0", | ||
"rimraf": "2.6.1", | ||
"sinon": "2.2.0" | ||
"babel-plugin-transform-object-rest-spread": "6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-flow": "^6.23.0", | ||
"chai": "4.1.2", | ||
"cross-env": "^5.1.4", | ||
"eslint": "4.19.0", | ||
"eslint-plugin-flowtype": "^2.46.1", | ||
"eslint-plugin-jest": "^21.15.0", | ||
"flow-bin": "0.68.0", | ||
"flow-copy-source": "1.3.0", | ||
"jest": "^22.4.2", | ||
"rimraf": "2.6.2" | ||
}, | ||
"scripts": { | ||
"test": "yarn run lint && yarn run typecheck && yarn run test:fast", | ||
"test:fast": "mocha test --compilers js:babel-core/register --globals global", | ||
"validate": "yarn run lint && yarn run typecheck", | ||
"test": "cross-env NODE_ENV=test jest", | ||
"typecheck": "flow check", | ||
"lint": "eslint src test -", | ||
"lint:fix": "yarn run lint --fix", | ||
"build": "yarn run build:clean && yarn run build:lib && yarn run build:flow", | ||
"build:clean": "rimraf lib", | ||
"build:lib": "babel src --out-dir lib", | ||
"build:flow": "flow-copy-source --verbose src lib", | ||
"coverage": "yarn run coverage:analyise && yarn run coverage:report && yarn run coverage:publish", | ||
"coverage:analyise": "nyc --check-coverage --statements 100 --branches 100 --functions 100 --lines 100 yarn run test:fast", | ||
"coverage:report": "nyc report --reporter=text-lcov > coverage.lcov", | ||
"coverage:publish": "codecov", | ||
"build": "yarn run build:clean && yarn run build:lib && yarn run build:esm && yarn run build:flow", | ||
"build:clean": "rimraf lib esm", | ||
"build:lib": "cross-env NODE_ENV=cjs babel src -d lib", | ||
"build:esm": "babel src --out-dir esm", | ||
"build:flow": "flow-copy-source --verbose src lib && flow-copy-source --verbose src esm", | ||
"prepublish": "yarn run build" | ||
}, | ||
"keywords": [ | ||
"memoize", | ||
"cache", | ||
"performance" | ||
] | ||
} | ||
} |
@@ -5,3 +5,3 @@ # memoizeOne | ||
[![Build Status](https://travis-ci.org/alexreardon/memoize-one.svg?branch=master)](https://travis-ci.org/alexreardon/memoize-one) [![codecov](https://codecov.io/gh/alexreardon/memoize-one/branch/master/graph/badge.svg)](https://codecov.io/gh/alexreardon/memoize-one) [![dependencies](https://david-dm.org/alexreardon/memoize-one.svg)](https://david-dm.org/alexreardon/memoize-one) [![SemVer](https://img.shields.io/badge/SemVer-2.0.0-brightgreen.svg)](http://semver.org/spec/v2.0.0.html) | ||
[![Build Status](https://travis-ci.org/alexreardon/memoize-one.svg?branch=master)](https://travis-ci.org/alexreardon/memoize-one) [![dependencies](https://david-dm.org/alexreardon/memoize-one.svg)](https://david-dm.org/alexreardon/memoize-one) [![SemVer](https://img.shields.io/badge/SemVer-2.0.0-brightgreen.svg)](http://semver.org/spec/v2.0.0.html) | ||
@@ -18,3 +18,3 @@ ## Rationale | ||
Unlike other memoization libraries, `memoizeOne` only remembers the latest arguments and result. No need to worry about cache busting mechanisms such as `maxAge`, `maxSize`, `exlusions` and so on which can be prone to memory leaks. `memoizeOne` simply remembers the last arguments, and if the function is next called with the same arguments then it returns the previous result. | ||
Unlike other memoization libraries, `memoizeOne` 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. `memoizeOne` simply remembers the last arguments, and if the function is next called with the same arguments then it returns the previous result. | ||
@@ -50,2 +50,3 @@ ## Usage | ||
### Custom equality function | ||
You can also pass in a custom function for checking the equality of two items. | ||
@@ -72,5 +73,7 @@ | ||
``` | ||
[Play with this example](http://www.webpackbin.com/NJW-tJMdf) | ||
#### Type signature | ||
#### Equality function type signature | ||
Here is the expected [flow](http://flowtype.org) type signature for a custom equality function: | ||
@@ -82,2 +85,33 @@ | ||
### Dynamic properties | ||
The result function will have the same [`.length` property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length) as the provided function. | ||
```js | ||
const add = (a, b) => a + b; | ||
const memoizedAdd = memoizeOne(add); | ||
memoizedAdd.length === 2; // true | ||
``` | ||
- For debug purposes we add a [`.name` property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) to the result function. If provided function has a name then the name will be `memoized_${yourFunction.name}`. Otherwise it will be `memoized_fn`. This assists in debugging memoized functions. | ||
```js | ||
// function has a name | ||
const add = (a, b) => a + b; | ||
// the original name is 'add' | ||
add.name === 'add'; // true | ||
// our new memoizedAdd function has a prefixed name | ||
const memoizedAdd = memoizeOne(add); | ||
memoizedAdd.name === 'memoized_add'; // true | ||
``` | ||
```js | ||
// function does not have a name | ||
const memoizedInline = memoizeOne((a, b) => a + b); | ||
memoizedInline.name === 'memoized_fn'; | ||
``` | ||
## Installation | ||
@@ -103,3 +137,3 @@ | ||
If you are in a CommonJS environment (eg [Node](https://nodejs.org)), then **you will need add `.default` to your import**: | ||
If you are in a CommonJS environment (eg [Node](https://nodejs.org)), then **you will need to add `.default` to your import**: | ||
@@ -125,3 +159,3 @@ ```js | ||
Changes to the running context (`this`) of a function can result in the function returning a different value event though its arguments have stayed the same: | ||
Changes 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: | ||
@@ -151,8 +185,11 @@ ```js | ||
### Tiny | ||
`memoizeOne` is super lightweight at `457 bytes` minified and `299 bytes` gzipped. (`1kb` = `1000 bytes`) | ||
### Extremely fast | ||
`memoizeOne` performs better or on par with than other popular memoization libraries for the purpose of remembering the latest invocation. | ||
**Results** | ||
- [simple arguments](https://www.measurethat.net/Benchmarks/ShowResult/4452) | ||
@@ -166,6 +203,6 @@ - [complex arguments](https://www.measurethat.net/Benchmarks/ShowResult/4488) | ||
- Tested with all built in [JavaScript types](https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch1.md). | ||
- [100% code coverage](https://codecov.io/gh/alexreardon/memoize-one). | ||
- 100% code coverage | ||
- [Continuous integration](https://travis-ci.org/alexreardon/memoize-one) to run tests and type checks. | ||
- [`Flow` types](http://flowtype.org) for safer internal execution and external consumption. Also allows for editor autocompletion. | ||
- Follows [Semantic versioning (2.0)](http://semver.org/) for safer consumption. | ||
- No dependencies | ||
- No dependencies |
@@ -6,2 +6,9 @@ // @flow | ||
const defineProperty = (target: Object, property: string, value: mixed) => | ||
Object.defineProperty(target, property, { | ||
writable: false, | ||
configurable: true, | ||
value: value, | ||
}); | ||
// <ResultFn: (...Array<any>) => mixed> | ||
@@ -37,4 +44,13 @@ // The purpose of this typing is to ensure that the returned memoized | ||
// Adding a length property | ||
// This is useful for some memoization checks that inspect the length of the function arguments | ||
defineProperty(result, 'length', resultFn.length); | ||
// Giving a useful name to the resulting function | ||
// This is helpful for debug purposes | ||
const name: string = `memoized_${resultFn.name || 'fn'}`; | ||
defineProperty(result, 'name', name); | ||
// telling flow to ignore the type of `result` as we know it is `ResultFn` | ||
return (result: any); | ||
} |
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
120
200
0
18680
8
1