Détail du package

react-fast-compare

FormidableLabs48.5mMIT3.2.2

Fastest deep equal comparison for React. Great for React.memo & shouldComponentUpdate. Also really fast general-purpose deep comparison.

fast, equal, react, compare

readme

React Fast Compare — Formidable, We build the modern web

Downloads Bundle Size GH Actions Status Coverage Status npm version Maintenance Status

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.

benchmark chart

(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 660 bytes minified+gzipped

Usage

const isEqual = require("react-fast-compare");

// general usage
console.log(isEqual({ foo: "bar" }, { foo: "bar" })); // true

// React.memo
// only re-render ExpensiveComponent when the props have deeply changed
const DeepMemoComponent = React.memo(ExpensiveComponent, isEqual);

// React.Component shouldComponentUpdate
// only re-render AnotherExpensiveComponent when the props have deeply changed
class 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 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!

Benchmarking this Library

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.

The results below are from a local test on a laptop (stats last updated 6/2/2020):

Generic Data

react-fast-compare x 177,600 ops/sec ±1.73% (92 runs sampled)
fast-deep-equal x 184,211 ops/sec ±0.65% (87 runs sampled)
lodash.isEqual x 39,826 ops/sec ±1.32% (86 runs sampled)
nano-equal x 176,023 ops/sec ±0.89% (92 runs sampled)
shallow-equal-fuzzy x 146,355 ops/sec ±0.64% (89 runs sampled)
  fastest: fast-deep-equal

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 86,392 ops/sec ±0.70% (93 runs sampled)
fast-deep-equal x 85,567 ops/sec ±0.95% (92 runs sampled)
lodash.isEqual x 7,369 ops/sec ±1.78% (84 runs sampled)
  fastest: react-fast-compare,fast-deep-equal

Two of these packages cannot handle comparing React elements, because they contain circular reference: nano-equal and shallow-equal-fuzzy.

Running Benchmarks

$ yarn install
$ yarn run benchmark

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.1.

Bundle Size

There are a variety of ways to calculate bundle size for JavaScript code. You can see our size test code in the compress script in package.json. Bundlephobia's calculation is slightly higher, as they do not mangle during minification.

License

MIT

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.

changelog

Changelog

3.2.2

Patch Changes

  • Adding GitHub release workflow (#126)

3.2.1 (2023-03-16)

Bugfixes:

  • Fix Object with null prototype errors #64.

3.2.0 (2020-05-28)

  • #80. Update types to use generic anys.
  • #77. Add tests for our TypeScript type definitions.

3.1.0 (2020-05-08)

  • #76. Add support for preact/compat.
  • #75. Drop test support for Node 8.
  • #62. Fix TypeScript types by declaring a function instead of a module.

3.0.2 (2020-05-01)

  • #71. Extend the hasArrayBuffer check to support older IE 11 versions.

3.0.1 (2020-02-05)

  • #60. Update documentation on bundle size.

3.0.0 (2020-01-05)

Features:

  • #36. Update to fast-deep-equal@3.1.1 with modified support for ES.next data types: Map, Set, ArrayBuffer.
  • #57. Minor refactoring to reduce min+gz size.
  • #59. Rename exported to isEqual for TypeScript users.

Breaking changes:

  • instances of different classes are now considered unequal
  • support for ES6 Map and Set instances
  • support for ES6 typed arrays

Infrastructure:

  • Upgrade lots of devDependenices
  • Use fast-deep-equal tests directly in our correctness tests.
  • Update CI to modern Node.js versions.
  • Update Appveyor to use straight IE11 (not emulated IE9) because mocha no longer runs in IE9.

2.0.4 (2018-11-09)

  • #39. Fix react-native bug introduced by DOM element checking.

2.0.3 (2018-11-08)

  • #33. Add handling for DOM elements. Thanks @viper1104!

2.0.2 (2018-08-21)

  • #28. Fix for localized versions of IE11. Thanks @excentrik!
  • #34. Fix typo. Thanks @Marviel!

2.0.1 (2018-06-25)

  • #26. Remove _store check. Thanks @chen-ye!

Major bugfix: Fixes RangeError in production, #25

2.0.0 (2018-06-04)

  • #21. Upgrade to fast-deep-equal@2.0.1. Thanks @samwhale!

Breaking changes:

  • null and Object comparison
  • new behavior: functions are no longer treated as equal
  • new behavior: handle NaN

1.0.0 (2018-04-12)

  • Initial release. forked from fast-deep-equal@1.1.0