Socket
Socket
Sign inDemoInstall

react-fast-compare

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-fast-compare

Fastest deep equal comparison for React. Perfect for shouldComponentUpdate. Also really fast general-purpose deep comparison


Version published
Weekly downloads
9.3M
increased by1.68%
Maintainers
1
Install size
9.05 kB
Created
Weekly downloads
 

Package description

What is react-fast-compare?

The react-fast-compare package is a fast deep equal comparison for React. It is optimized for comparing the props or state of React components, although it can be used to compare any data structures. It is particularly useful in shouldComponentUpdate methods to determine if re-rendering is necessary.

What are react-fast-compare's main functionalities?

Deep comparison of objects and arrays

This feature allows for deep comparison of objects and arrays to determine if they are structurally identical.

import isEqual from 'react-fast-compare';

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };

const areEqual = isEqual(obj1, obj2); // true

Comparison within React lifecycle methods

This code sample demonstrates how react-fast-compare can be used within a React component's shouldComponentUpdate lifecycle method to prevent unnecessary re-renders.

import React from 'react';
import isEqual from 'react-fast-compare';

class MyComponent extends React.Component {
  shouldComponentUpdate(nextProps) {
    return !isEqual(this.props, nextProps);
  }

  render() {
    // Component rendering logic
  }
}

Other packages similar to react-fast-compare

Changelog

Source

1.0.0 (2018-04-12)

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

Readme

Source

react-fast-compare

The fastest deep equal comparison for React, perfect for shouldComponentUpdate, also really fast at general-purpose deep comparison. This is a fork of the brilliant fast-deep-equal with some extra handling for React.

Build Status npm version

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 be just as fast as fast-deep-equal for general use, and faster for React use

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() {
    // ...
  }
}

Benchmarking

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 161,872 ops/sec ±1.18% (82 runs sampled)
fast-deep-equal x 159,889 ops/sec ±1.62% (85 runs sampled)
lodash.isEqual x 30,750 ops/sec ±2.02% (86 runs sampled)
nano-equal x 35,608 ops/sec ±1.55% (86 runs sampled)
shallow-equal-fuzzy x 94,141 ops/sec ±1.80% (89 runs sampled)
  fastest: react-fast-compare,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 150,667 ops/sec ±1.86% (83 runs sampled)
fast-deep-equal x 510 ops/sec ±1.67% (77 runs sampled)
lodash.isEqual x 25,760 ops/sec ±1.63% (83 runs sampled)
nano-equal x 629 ops/sec ±2.43% (80 runs sampled)
shallow-equal-fuzzy x 454 ops/sec ±1.42% (79 runs sampled)
  fastest: react-fast-compare

Three of these packages cannot handle comparing React elements (which are circular): fast-deep-equal, nano-equal, and shallow-equal-fuzzy.

Running Benchmarks

yarn install
yarn run benchmark

License

MIT

Keywords

FAQs

Last updated on 12 Apr 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc