deep-diff vs diff vs diff-match-patch vs diff2html vs react-diff-view
JavaScript Diff Libraries
deep-diffdiffdiff-match-patchdiff2htmlreact-diff-viewSimilar Packages:

JavaScript Diff Libraries

These libraries are designed to compare and highlight differences between data structures, text, or documents. They serve various purposes, including visualizing changes in code, tracking modifications in data, and facilitating collaborative editing by providing clear insights into what has changed. Each library has its unique features and use cases, making them suitable for different scenarios in web development.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
deep-diff00-08 years agoMIT
diff09,089510 kB192 months agoBSD-3-Clause
diff-match-patch0273-86 years agoApache-2.0
diff2html03,3272.02 MB25a month agoMIT
react-diff-view09911.48 MB118 months agoMIT

Feature Comparison: deep-diff vs diff vs diff-match-patch vs diff2html vs react-diff-view

Comparison Depth

  • deep-diff:

    deep-diff provides deep comparison capabilities, allowing you to detect changes in nested objects and arrays. It can identify added, removed, or modified properties at any level of depth, making it suitable for complex data structures.

  • diff:

    diff offers a simple line-by-line comparison, focusing on text differences without delving into nested structures. It is best for straightforward text comparisons, such as comparing two versions of a document.

  • diff-match-patch:

    diff-match-patch excels in comparing large blocks of text efficiently, using algorithms that minimize the number of operations needed to find differences. It is designed for handling extensive text data while providing accurate results.

  • diff2html:

    diff2html does not perform comparisons itself but formats the output of diff tools into HTML. It transforms the diff output into a visually appealing format, making it easier to read and understand the differences.

  • react-diff-view:

    react-diff-view provides a React component that visualizes diffs, focusing on user-friendly presentation. It is tailored for React applications, allowing developers to easily integrate diff views into their UI.

Use Case Scenarios

  • deep-diff:

    deep-diff is ideal for applications that require tracking changes in application state, such as Redux or MobX, where understanding the evolution of state is crucial for debugging and state management.

  • diff:

    diff is suitable for simple text comparison tasks, such as comparing configuration files or documents where line-by-line differences are sufficient.

  • diff-match-patch:

    diff-match-patch is perfect for collaborative applications like text editors or version control systems, where users need to see changes in real-time and apply patches to documents.

  • diff2html:

    diff2html is best used in scenarios where you need to present diff outputs from version control systems or other diff tools in a web application, enhancing the user experience with clear visual representation.

  • react-diff-view:

    react-diff-view is designed for React applications that need to display differences between text or code snippets, making it ideal for code review tools or collaborative coding platforms.

Performance

  • deep-diff:

    deep-diff is optimized for performance when dealing with deep comparisons, but may become slower with very large and deeply nested structures due to its recursive nature.

  • diff:

    diff is lightweight and performs well for basic text comparisons, making it suitable for quick checks without significant performance overhead.

  • diff-match-patch:

    diff-match-patch is highly efficient for large text comparisons, using advanced algorithms to minimize processing time and memory usage, making it suitable for applications handling substantial text data.

  • diff2html:

    diff2html's performance is contingent on the diff output it processes; it efficiently formats the output but does not perform comparisons itself, so its speed is primarily based on the underlying diff tool used.

  • react-diff-view:

    react-diff-view is optimized for rendering diffs in React applications, ensuring smooth performance even with larger diffs, but may require optimization for extremely large datasets.

Integration

  • deep-diff:

    deep-diff can be easily integrated into any JavaScript application, especially those using state management libraries, to track and visualize changes in application state.

  • diff:

    diff is a standalone library that can be integrated into any JavaScript project for basic text comparison tasks without dependencies.

  • diff-match-patch:

    diff-match-patch can be integrated into various applications, including web and mobile, providing a robust solution for text comparison and patching.

  • diff2html:

    diff2html is designed to work with the output of diff tools, making it easy to integrate into existing workflows where diff outputs need to be presented in a web format.

  • react-diff-view:

    react-diff-view is specifically built for React applications, allowing seamless integration into React components for displaying diffs.

Learning Curve

  • deep-diff:

    deep-diff has a moderate learning curve due to its focus on deep comparisons and the need to understand its output format for effective use.

  • diff:

    diff is straightforward and easy to use, making it accessible for developers with basic JavaScript knowledge.

  • diff-match-patch:

    diff-match-patch may require some understanding of algorithms and text processing concepts, but it is well-documented and relatively easy to grasp for those familiar with JavaScript.

  • diff2html:

    diff2html is simple to use, especially for developers familiar with diff outputs; it requires minimal effort to integrate and display diffs in HTML format.

  • react-diff-view:

    react-diff-view is designed for React developers, making it easy to adopt for those already familiar with React concepts and component-based architecture.

How to Choose: deep-diff vs diff vs diff-match-patch vs diff2html vs react-diff-view

  • deep-diff:

    Choose deep-diff if you need to perform deep comparisons of JavaScript objects and arrays, particularly when you want to track changes in complex nested structures. It provides detailed change information, including additions, deletions, and modifications, making it ideal for state management in applications.

  • diff:

    Select diff for a straightforward line-by-line comparison of text files or strings. It is lightweight and efficient, making it suitable for applications that require basic diff functionalities without additional overhead.

  • diff-match-patch:

    Opt for diff-match-patch if you require a robust solution for comparing and patching text. This library is particularly useful for applications like collaborative text editors where you need to visualize changes and apply patches efficiently, handling large texts with ease.

  • diff2html:

    Use diff2html when you need to convert diff outputs into HTML format for better visualization. It is perfect for displaying diffs in a user-friendly way, making it suitable for web applications that need to present changes in a clear and structured manner.

  • react-diff-view:

    Choose react-diff-view if you are working with React and need to display diffs within your application. This library provides a React component that renders diffs in a visually appealing manner, making it ideal for applications that require inline diff visualization.

README for deep-diff

deep-diff

CircleCI

NPM

deep-diff is a javascript/node.js module providing utility functions for determining the structural differences between objects and includes some utilities for applying differences across objects.

Install

npm install deep-diff

Possible v1.0.0 incompatabilities:

  • elements in arrays are now processed in reverse order, which fixes a few nagging bugs but may break some users
    • If your code relied on the order in which the differences were reported then your code will break. If you consider an object graph to be a big tree, then deep-diff does a pre-order traversal of the object graph, however, when it encounters an array, the array is processed from the end towards the front, with each element recursively processed in-order during further descent.

Features

  • Get the structural differences between two objects.
  • Observe the structural differences between two objects.
  • When structural differences represent change, apply change from one object to another.
  • When structural differences represent change, selectively apply change from one object to another.

Installation

npm install deep-diff

Importing

nodejs

var diff = require('deep-diff')
// or:
// const diff = require('deep-diff');
// const { diff } = require('deep-diff');
// or:
// const DeepDiff = require('deep-diff');
// const { DeepDiff } = require('deep-diff');
// es6+:
// import diff from 'deep-diff';
// import { diff } from 'deep-diff';
// es6+:
// import DeepDiff from 'deep-diff';
// import { DeepDiff } from 'deep-diff';

browser

<script src="https://cdn.jsdelivr.net/npm/deep-diff@1/dist/deep-diff.min.js"></script>

In a browser, deep-diff defines a global variable DeepDiff. If there is a conflict in the global namespace you can restore the conflicting definition and assign deep-diff to another variable like this: var deep = DeepDiff.noConflict();.

Simple Examples

In order to describe differences, change revolves around an origin object. For consistency, the origin object is always the operand on the left-hand-side of operations. The comparand, which may contain changes, is always on the right-hand-side of operations.

var diff = require('deep-diff').diff;

var lhs = {
  name: 'my object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'elements']
  }
};

var rhs = {
  name: 'updated object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'more', 'elements', { than: 'before' }]
  }
};

var differences = diff(lhs, rhs);

v 0.2.0 and above The code snippet above would result in the following structure describing the differences:

[ { kind: 'E',
    path: [ 'name' ],
    lhs: 'my object',
    rhs: 'updated object' },
  { kind: 'E',
    path: [ 'details', 'with', 2 ],
    lhs: 'elements',
    rhs: 'more' },
  { kind: 'A',
    path: [ 'details', 'with' ],
    index: 3,
    item: { kind: 'N', rhs: 'elements' } },
  { kind: 'A',
    path: [ 'details', 'with' ],
    index: 4,
    item: { kind: 'N', rhs: { than: 'before' } } } ]

Differences

Differences are reported as one or more change records. Change records have the following structure:

  • kind - indicates the kind of change; will be one of the following:
    • N - indicates a newly added property/element
    • D - indicates a property/element was deleted
    • E - indicates a property/element was edited
    • A - indicates a change occurred within an array
  • path - the property path (from the left-hand-side root)
  • lhs - the value on the left-hand-side of the comparison (undefined if kind === 'N')
  • rhs - the value on the right-hand-side of the comparison (undefined if kind === 'D')
  • index - when kind === 'A', indicates the array index where the change occurred
  • item - when kind === 'A', contains a nested change record indicating the change that occurred at the array index

Change records are generated for all structural differences between origin and comparand. The methods only consider an object's own properties and array elements; those inherited from an object's prototype chain are not considered.

Changes to arrays are recorded simplistically. We care most about the shape of the structure; therefore we don't take the time to determine if an object moved from one slot in the array to another. Instead, we only record the structural differences. If the structural differences are applied from the comparand to the origin then the two objects will compare as "deep equal" using most isEqual implementations such as found in lodash or underscore.

Changes

When two objects differ, you can observe the differences as they are calculated and selectively apply those changes to the origin object (left-hand-side).

var observableDiff = require('deep-diff').observableDiff;
var applyChange = require('deep-diff').applyChange;

var lhs = {
  name: 'my object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'elements']
  }
};

var rhs = {
  name: 'updated object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'more', 'elements', { than: 'before' }]
};

observableDiff(lhs, rhs, function (d) {
  // Apply all changes except to the name property...
  if (d.path[d.path.length - 1] !== 'name') {
    applyChange(lhs, rhs, d);
  }
});

API Documentation

A standard import of var diff = require('deep-diff') is assumed in all of the code examples. The import results in an object having the following public properties:

  • diff(lhs, rhs, prefilter, acc) — calculates the differences between two objects, optionally prefiltering elements for comparison, and optionally using the specified accumulator.
  • observableDiff(lhs, rhs, observer, prefilter) — calculates the differences between two objects and reports each to an observer function, optionally, prefiltering elements for comparison.
  • applyDiff(target, source, filter) — applies any structural differences from a source object to a target object, optionally filtering each difference.
  • applyChange(target, source, change) — applies a single change record to a target object. NOTE: source is unused and may be removed.
  • revertChange(target, source, change) reverts a single change record to a target object. NOTE: source is unused and may be removed.

diff

The diff function calculates the difference between two objects.

Arguments

  • lhs - the left-hand operand; the origin object.
  • rhs - the right-hand operand; the object being compared structurally with the origin object.
  • prefilter - an optional function that determines whether difference analysis should continue down the object graph.
  • acc - an optional accumulator/array (requirement is that it have a push function). Each difference is pushed to the specified accumulator.

Returns either an array of changes or, if there are no changes, undefined. This was originally chosen so the result would be pass a truthy test:

var changes = diff(obja, objb);
if (changes) {
  // do something with the changes.
}

Pre-filtering Object Properties

The prefilter's signature should be function(path, key) and it should return a truthy value for any path-key combination that should be filtered. If filtered, the difference analysis does no further analysis of on the identified object-property path.

const diff = require('deep-diff');
const assert = require('assert');

const data = {
  issue: 126,
  submittedBy: 'abuzarhamza',
  title: 'readme.md need some additional example prefilter',
  posts: [
    {
      date: '2018-04-16',
      text: `additional example for prefilter for deep-diff would be great.
      https://stackoverflow.com/questions/38364639/pre-filter-condition-deep-diff-node-js`
    }
  ]
};

const clone = JSON.parse(JSON.stringify(data));
clone.title = 'README.MD needs additional example illustrating how to prefilter';
clone.disposition = 'completed';

const two = diff(data, clone);
const none = diff(data, clone,
  (path, key) => path.length === 0 && ~['title', 'disposition'].indexOf(key)
);

assert.equal(two.length, 2, 'should reflect two differences');
assert.ok(typeof none === 'undefined', 'should reflect no differences');

Contributing

When contributing, keep in mind that it is an objective of deep-diff to have no package dependencies. This may change in the future, but for now, no-dependencies.

Please run the unit tests before submitting your PR: npm test. Hopefully your PR includes additional unit tests to illustrate your change/modification!

When you run npm test, linting will be performed and any linting errors will fail the tests... this includes code formatting.

Thanks to all those who have contributed so far!