- Update library to include ES.next support for `Map`, `Set`, `ArrayBuffer`. Part of #36 - Update to `fast-deep-equal@3.1.1` with modified support for ES.next data types. - Upgrade lots of `devDependenices` - Use `fast-deep-equal` tests directly in our correctness tests. - Update CI to modern Node.js versions. - **Note**: There's a bug / limitation of `Set` comparisons whereby objects are compared by reference not value. Tracked at #50 . In our `yarn benchmark`, `lodash.isEqual` gets test differences because it correctly handles those.
react-fast-compare
The fastest deep equal comparison for React. Really fast general-purpose deep comparison.
Great forshouldComponentUpdate. This is a fork of the brilliant
fast-deep-equal with some
extra handling for React.
(Check out the benchmarking details.)
Install
$ yarn add react-fast-compare
# or
$ npm install react-fast-compare
Highlights
- ES5 compatible; works in node.js (0.10+) and browsers (IE9+)
- deeply compares any value (besides objects with circular references)
- handles React-specific circular references, like elements
- checks equality Date and RegExp objects
- should as fast as fast-deep-equal via a single unified library, and with added guardrails for circular references.
- small: under 700 bytes minified+gzipped
Usage
const isEqual = require("react-fast-compare");
// general usage
console.log(isEqual({ foo: "bar" }, { foo: "bar" })); // true
// react usage
class ExpensiveRenderer extends React.Component {
shouldComponentUpdate(nextProps) {
return !isEqual(this.props, nextProps);
}
render() {
// ...
}
}
Do I Need shouldComponentUpdate?
What's faster than a really fast deep comparison? No deep comparison at all.
—This Readme
Deep checks in React's shouldComponentUpdate should not be used blindly.
First, see if a
PureComponent
would work for you. If it won't (if you need deep checks), it's wise to make
sure you've correctly indentified the bottleneck in your application by
profiling the performance.
After you've determined that you do need deep equality checks and you've
identified the minimum number of places to apply them, then this library may
be for you! For more information about making your app faster, check out the
Optimizing Performance
section of the React docs.
Benchmarking this Library
All tests carried out locally on a MacBook. The absolute values are much less important than the relative differences between packages.
Benchmarking source can be found here. Each "operation" consists of running all relevant tests. The React benchmark uses both the generic tests and the react tests; these runs will be slower simply because there are more tests in each operation.
Generic Data
react-fast-compare x 157,863 ops/sec ±0.54% (94 runs sampled)
fast-deep-equal x 149,877 ops/sec ±0.76% (93 runs sampled)
lodash.isEqual x 33,298 ops/sec ±0.70% (93 runs sampled)
nano-equal x 144,836 ops/sec ±0.51% (94 runs sampled)
shallow-equal-fuzzy x 110,192 ops/sec ±0.57% (95 runs sampled)
fastest: react-fast-compare
react-fast-compare and fast-deep-equal should be the same speed for these
tests; any difference is just noise. react-fast-compare won't be faster than
fast-deep-equal, because it's based on it.
React and Generic Data
react-fast-compare x 64,102 ops/sec ±0.36% (94 runs sampled)
fast-deep-equal x 63,844 ops/sec ±0.43% (94 runs sampled)
lodash.isEqual x 6,243 ops/sec ±0.72% (90 runs sampled)
fastest: react-fast-compare,fast-deep-equal
Two of these packages cannot handle comparing React elements (which are
circular): nano-equal and shallow-equal-fuzzy.
Running Benchmarks
$ yarn install
$ yarn run benchmark
fast-deep-equal Versioning
react-fast-compare@3 tracks fast-deep-equal@3.1.1
Now that fast-deep-equal has separate es5, es6, and es6 + React entry points, the main differences with this library are:
try/catchguardrails for stack overflows from undetected circular references.- A single unified entry point for all uses. No matter what your target application is,
import equal fro 'react-fast-compare'just works.
License
Contributing
Please see our contributions guide.
Maintenance Status
Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.
