deep-diff vs jsondiffpatch vs json-diff
JavaScriptの差分比較ライブラリ
deep-diffjsondiffpatchjson-diff類似パッケージ:

JavaScriptの差分比較ライブラリ

これらのライブラリは、オブジェクトやJSONデータの差分を比較し、変更点を特定するために使用されます。特に、データのバージョン管理や変更の追跡が必要なアプリケーションにおいて、データの変更を視覚的に表示したり、差分を適用したりする際に役立ちます。これにより、開発者はデータの変更を簡単に理解し、適切に対応することができます。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
deep-diff2,671,6160-08年前MIT
jsondiffpatch2,423,2655,288159 kB511年前MIT
json-diff863,1951,20439.6 kB323年前MIT

機能比較: deep-diff vs jsondiffpatch vs json-diff

比較の深さ

  • deep-diff:

    deep-diffは、オブジェクトの深い比較を行い、ネストされたプロパティや配列の変更を詳細に追跡します。これにより、複雑なデータ構造の変更を正確に把握することができます。

  • jsondiffpatch:

    jsondiffpatchは、JSONデータの差分を視覚的に表示し、変更を適用する機能を持っています。深い比較も可能ですが、主に視覚的な差分表示に重点を置いています。

  • json-diff:

    json-diffは、主にJSONデータの比較に特化しており、シンプルな構造のデータに対して効果的です。深い比較は行わず、表面的な変更に焦点を当てています。

視覚化機能

  • deep-diff:

    deep-diffは、変更点を詳細に示すためのデータ構造を提供しますが、視覚化機能は組み込まれていません。ユーザーは、結果を自分で視覚化する必要があります。

  • jsondiffpatch:

    jsondiffpatchは、変更を視覚的に表示するための強力な機能を持っており、変更を適用する際の視覚的フィードバックを提供します。これにより、ユーザーは変更の影響を直感的に理解できます。

  • json-diff:

    json-diffは、変更点を簡潔に表示するための視覚化機能を提供します。特に、変更の概要を迅速に把握するのに役立ちます。

パフォーマンス

  • deep-diff:

    deep-diffは、深い比較を行うため、複雑なオブジェクトに対してはパフォーマンスが低下する可能性があります。特に大規模なデータセットを扱う場合は注意が必要です。

  • jsondiffpatch:

    jsondiffpatchは、視覚化機能を持つため、パフォーマンスがやや低下する可能性がありますが、変更の適用や元に戻す機能があるため、ユーザー体験は向上します。

  • json-diff:

    json-diffは、シンプルな比較を行うため、パフォーマンスが高く、迅速に結果を返します。小規模なJSONデータに対しては非常に効率的です。

使いやすさ

  • deep-diff:

    deep-diffは、APIがシンプルで直感的ですが、深い比較を行うためには、データ構造を理解している必要があります。

  • jsondiffpatch:

    jsondiffpatchは、視覚的な差分表示を提供するため、使いやすさが高いですが、機能が豊富であるため、学習コストがややかかる場合があります。

  • json-diff:

    json-diffは、シンプルなAPIを提供しており、使いやすさが高いです。特に、JSONデータを扱う際に直感的に使用できます。

拡張性

  • deep-diff:

    deep-diffは、カスタマイズ可能な比較ロジックを提供しており、特定のニーズに応じて拡張することができます。

  • jsondiffpatch:

    jsondiffpatchは、変更の適用や元に戻す機能を持っており、拡張性が高いです。特に、視覚化機能をカスタマイズすることが可能です。

  • json-diff:

    json-diffは、シンプルな比較機能に特化しているため、拡張性は限られていますが、特定の用途には非常に効果的です。

選び方: deep-diff vs jsondiffpatch vs json-diff

  • deep-diff:

    deep-diffは、オブジェクトの深い比較を必要とする場合に最適です。特に、ネストされたオブジェクトや配列の変更を詳細に追跡したい場合に選択してください。

  • jsondiffpatch:

    jsondiffpatchは、JSONデータの差分を視覚的に表示し、変更を適用する機能を持っています。変更を適用したり、元に戻したりする機能が必要な場合に選択してください。

  • json-diff:

    json-diffは、JSONデータの比較に特化しており、シンプルな差分表示を提供します。JSON形式のデータを扱う場合や、視覚的な差分表示が重要な場合に適しています。

deep-diff のREADME

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!