react-fast-compare

fastest deep equal comparison for React

  • 所有者: FormidableLabs/react-fast-compare
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

react-fast-compare

Downloads
Bundle Size
Travis Status
AppVeyor 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 650 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.

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

主要指標

概覽
名稱與所有者FormidableLabs/react-fast-compare
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2018-04-10 18:33:39
推送於2024-03-20 20:11:52
最后一次提交2024-03-20 13:11:51
發布數17
最新版本名稱v3.2.2 (發布於 2023-05-18 19:17:10)
第一版名稱v0.1.0 (發布於 2017-06-16 22:55:08)
用户参与
星數1.7k
關注者數45
派生數56
提交數157
已啟用問題?
問題數38
打開的問題數7
拉請求數65
打開的拉請求數5
關閉的拉請求數28
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?