Gathering detailed insights and metrics for deep-diff
Gathering detailed insights and metrics for deep-diff
Gathering detailed insights and metrics for deep-diff
Gathering detailed insights and metrics for deep-diff
deep-object-diff
Deep diffs two objects, including nested structures of arrays and objects, and return the difference.
@types/deep-diff
TypeScript definitions for deep-diff
json-diff-ts
Modern TypeScript JSON diff library - Zero dependencies, high performance, ESM + CommonJS support. Calculate and apply differences between JSON objects with advanced features like key-based array diffing, JSONPath support, and atomic changesets.
@gthrm/deep-diff
A library for comparing nested objects and arrays to find the difference
Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
npm install deep-diff
Typescript
Module System
Node Version
NPM Version
JavaScript (97.64%)
HTML (2.36%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
3,039 Stars
222 Commits
215 Forks
36 Watchers
2 Branches
31 Contributors
Updated on Jul 12, 2025
Latest Version
1.0.2
Package Id
deep-diff@1.0.2
Size
118.27 kB
NPM Version
5.6.0
Node Version
8.11.3
Published on
Aug 16, 2018
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
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.
1npm install deep-diff
Possible v1.0.0 incompatabilities:
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.1npm install deep-diff
1var diff = require('deep-diff') 2// or: 3// const diff = require('deep-diff'); 4// const { diff } = require('deep-diff'); 5// or: 6// const DeepDiff = require('deep-diff'); 7// const { DeepDiff } = require('deep-diff'); 8// es6+: 9// import diff from 'deep-diff'; 10// import { diff } from 'deep-diff'; 11// es6+: 12// import DeepDiff from 'deep-diff'; 13// import { DeepDiff } from 'deep-diff';
1<script src="https://cdn.jsdelivr.net/npm/deep-diff@1/dist/deep-diff.min.js"></script>
In a browser,
deep-diff
defines a global variableDeepDiff
. If there is a conflict in the global namespace you can restore the conflicting definition and assigndeep-diff
to another variable like this:var deep = DeepDiff.noConflict();
.
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.
1var diff = require('deep-diff').diff; 2 3var lhs = { 4 name: 'my object', 5 description: 'it\'s an object!', 6 details: { 7 it: 'has', 8 an: 'array', 9 with: ['a', 'few', 'elements'] 10 } 11}; 12 13var rhs = { 14 name: 'updated object', 15 description: 'it\'s an object!', 16 details: { 17 it: 'has', 18 an: 'array', 19 with: ['a', 'few', 'more', 'elements', { than: 'before' }] 20 } 21}; 22 23var differences = diff(lhs, rhs);
v 0.2.0 and above The code snippet above would result in the following structure describing the differences:
1[ { kind: 'E', 2 path: [ 'name' ], 3 lhs: 'my object', 4 rhs: 'updated object' }, 5 { kind: 'E', 6 path: [ 'details', 'with', 2 ], 7 lhs: 'elements', 8 rhs: 'more' }, 9 { kind: 'A', 10 path: [ 'details', 'with' ], 11 index: 3, 12 item: { kind: 'N', rhs: 'elements' } }, 13 { kind: 'A', 14 path: [ 'details', 'with' ], 15 index: 4, 16 item: { kind: 'N', rhs: { than: 'before' } } } ]
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/elementD
- indicates a property/element was deletedE
- indicates a property/element was editedA
- indicates a change occurred within an arraypath
- 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 occurreditem
- when kind === 'A', contains a nested change record indicating the change that occurred at the array indexChange 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.
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).
1var observableDiff = require('deep-diff').observableDiff; 2var applyChange = require('deep-diff').applyChange; 3 4var lhs = { 5 name: 'my object', 6 description: 'it\'s an object!', 7 details: { 8 it: 'has', 9 an: 'array', 10 with: ['a', 'few', 'elements'] 11 } 12}; 13 14var rhs = { 15 name: 'updated object', 16 description: 'it\'s an object!', 17 details: { 18 it: 'has', 19 an: 'array', 20 with: ['a', 'few', 'more', 'elements', { than: 'before' }] 21}; 22 23observableDiff(lhs, rhs, function (d) { 24 // Apply all changes except to the name property... 25 if (d.path[d.path.length - 1] !== 'name') { 26 applyChange(lhs, rhs, d); 27 } 28});
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.
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:
1var changes = diff(obja, objb); 2if (changes) { 3 // do something with the changes. 4}
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.
1const diff = require('deep-diff'); 2const assert = require('assert'); 3 4const data = { 5 issue: 126, 6 submittedBy: 'abuzarhamza', 7 title: 'readme.md need some additional example prefilter', 8 posts: [ 9 { 10 date: '2018-04-16', 11 text: `additional example for prefilter for deep-diff would be great. 12 https://stackoverflow.com/questions/38364639/pre-filter-condition-deep-diff-node-js` 13 } 14 ] 15}; 16 17const clone = JSON.parse(JSON.stringify(data)); 18clone.title = 'README.MD needs additional example illustrating how to prefilter'; 19clone.disposition = 'completed'; 20 21const two = diff(data, clone); 22const none = diff(data, clone, 23 (path, key) => path.length === 0 && ~['title', 'disposition'].indexOf(key) 24); 25 26assert.equal(two.length, 2, 'should reflect two differences'); 27assert.ok(typeof none === 'undefined', 'should reflect no differences');
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!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/19 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
43 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More