The fastest deep equal comparison for React. Very quick general-purpose deep
comparison, too. Great for React.memo and shouldComponentUpdate.
This is a fork of the brilliant
fast-deep-equal with some
extra handling for React.
Install
$ 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 of Date and RegExp objects
should be as fast as fast-deep-equal via a single unified library, and with added guardrails for circular references.
small: under 660 bytes minified+gzipped
Usage
const isEqual = require('react-fast-compare');// general usageconsole.log(isEqual({ foo: 'bar' }, { foo: 'bar' })); // true// React.memo// only re-render ExpensiveComponent when the props have deeply changedconst DeepMemoComponent = React.memo(ExpensiveComponent, isEqual);// React.Component shouldComponentUpdate// only re-render AnotherExpensiveComponent when the props have deeply changedclass AnotherExpensiveComponent extends React.Component { shouldComponentUpdate(nextProps) { return !isEqual(this.props, nextProps); } render() { // ... }
Do I Need React.memo (or shouldComponentUpdate)?
What's faster than a really fast deep comparison? No deep comparison at all.
—This Readme
Deep checks in React.memo or a shouldComponentUpdate should not be used blindly.
First, see if the default
React.memo or
PureComponent
will work for you. If it won't (if you need deep checks), it's wise to make
sure you've correctly identified 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!
Benchmarking this Library
react-fast-compare is based on fast-deep-equal, and aims to be as fast on
generic data while also handling React elements. The benchmark
(bench/, Vitest's
benchmarking) compares this code with
the last published version and with fast-deep-equal, over the same test cases
the tests use:
$ pnpm install$ pnpm bench
Absolute numbers depend on the machine; compare rows from the same run. CI adds
the results to the job summary of each run.
Differences between this library and fast-deep-equal
react-fast-compare is based on fast-deep-equal, with some additions:
react-fast-compare has try/catch guardrails for stack overflows from undetected (non-React) circular references.
react-fast-compare has a single unified entry point for all uses. No matter what your target application is, import equal from 'react-fast-compare' just works. fast-deep-equal has multiple entry points for different use cases.
This version of react-fast-compare tracks fast-deep-equal@3.1.3.
Bundle Size
There are a variety of ways to calculate bundle size for JavaScript code.
pnpm size (scripts/size.mjs) minifies index.js with
terser, mangling top-level names, and reports the raw, minified, gzip and
brotli sizes. CI adds the same report to each run's job summary.
Bundlephobia's calculation is slightly higher,
as they do not mangle during minification.
Active: Nearform is actively working on this project, and we expect to continue work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.