microdiff
A fast, zero dependency object and array comparison library. Significantly faster than most other deep comparison libraries and has full TypeScript support.
Top Related Projects
Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
Diff & patch JavaScript objects
Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.
A javascript text differencing implementation.
fastest deep equal comparison for React
Quick Overview
Microdiff is a lightweight, zero-dependency JavaScript library for performing deep object comparisons. It efficiently detects and reports differences between two objects, making it useful for change detection in various applications, including state management and data synchronization.
Pros
- Extremely small size (less than 1KB minified and gzipped)
- Zero dependencies, reducing potential security risks and bloat
- Fast performance, optimized for quick comparisons
- Supports deep object and array comparisons
Cons
- Limited features compared to more comprehensive diff libraries
- May not handle complex data structures or custom object types as well as larger libraries
- No built-in visualization of differences
- Limited documentation and examples available
Code Examples
Comparing two objects:
import { diff } from "microdiff";
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 3 } };
console.log(diff(obj1, obj2));
// Output: [{ type: "CHANGE", path: ["b", "c"], value: 3, oldValue: 2 }]
Comparing arrays:
import { diff } from "microdiff";
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 4, 5];
console.log(diff(arr1, arr2));
// Output: [
// { type: "CHANGE", path: [2], value: 4, oldValue: 3 },
// { type: "ADD", path: [3], value: 5 }
// ]
Using options to ignore specific paths:
import { diff } from "microdiff";
const obj1 = { a: 1, b: { c: 2, d: 3 } };
const obj2 = { a: 1, b: { c: 3, d: 4 } };
console.log(diff(obj1, obj2, { ignorePaths: ["b.c"] }));
// Output: [{ type: "CHANGE", path: ["b", "d"], value: 4, oldValue: 3 }]
Getting Started
To use Microdiff in your project, first install it via npm:
npm install microdiff
Then, import and use it in your JavaScript or TypeScript code:
import { diff } from "microdiff";
const oldObj = { a: 1, b: [1, 2, 3] };
const newObj = { a: 2, b: [1, 2, 4] };
const differences = diff(oldObj, newObj);
console.log(differences);
This will output an array of difference objects, each describing a change, addition, or deletion between the two input objects.
Competitor Comparisons
Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
Pros of diff
- More mature project with longer development history
- Supports custom equality comparisons for complex objects
- Provides detailed change information, including array moves
Cons of diff
- Larger package size (about 8KB minified)
- Less actively maintained, with fewer recent updates
- More complex API, potentially steeper learning curve
Code Comparison
microdiff:
import { diff } from 'microdiff';
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 3, c: 4 };
console.log(diff(obj1, obj2));
diff:
var diff = require('diff');
var obj1 = { a: 1, b: 2 };
var obj2 = { a: 1, b: 3, c: 4 };
console.log(diff(obj1, obj2));
Both libraries provide a simple API for comparing objects, but microdiff uses ES6 module syntax while diff uses CommonJS. The output format and level of detail may differ between the two libraries, with diff potentially providing more comprehensive change information.
Diff & patch JavaScript objects
Pros of jsondiffpatch
- More comprehensive and feature-rich, offering advanced diffing and patching capabilities
- Supports custom equality checks and object hash functions
- Provides visual diff rendering for HTML output
Cons of jsondiffpatch
- Larger package size and potentially higher performance overhead
- More complex API, which may be overkill for simple use cases
- Less frequent updates and maintenance compared to microdiff
Code Comparison
microdiff:
import { diff } from 'microdiff';
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 3, c: 4 };
console.log(diff(obj1, obj2));
jsondiffpatch:
import { create } from 'jsondiffpatch';
const jsondiffpatch = create();
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 3, c: 4 };
console.log(jsondiffpatch.diff(obj1, obj2));
Both libraries provide object diffing functionality, but jsondiffpatch offers more configuration options and advanced features. microdiff has a simpler API and is more lightweight, making it suitable for basic diffing needs. jsondiffpatch is better suited for complex scenarios requiring customization and visual diff rendering.
Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.
Pros of diff-match-patch
- More comprehensive and feature-rich, offering advanced text comparison algorithms
- Well-established and widely used in various applications
- Supports multiple programming languages (C++, Java, JavaScript, Lua, Objective C, Python)
Cons of diff-match-patch
- Larger codebase and potentially more complex to integrate
- May be overkill for simple diff operations
- Less focused on performance optimization for small-scale comparisons
Code Comparison
microdiff:
import { diff } from 'microdiff';
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 3, c: 4 };
console.log(diff(obj1, obj2));
diff-match-patch:
const dmp = new diff_match_patch();
const text1 = "Hello World.";
const text2 = "Goodbye World.";
const diffs = dmp.diff_main(text1, text2);
console.log(diffs);
Key Differences
- microdiff focuses on object and array comparison, while diff-match-patch specializes in text-based diff operations
- microdiff is designed for simplicity and performance in JavaScript environments, whereas diff-match-patch offers a more robust set of features across multiple languages
- microdiff provides a more modern and streamlined API, while diff-match-patch follows a more traditional approach to diff operations
A javascript text differencing implementation.
Pros of jsdiff
- More comprehensive and feature-rich, offering various diffing algorithms
- Well-established project with a larger community and longer history
- Supports multiple programming languages and file formats
Cons of jsdiff
- Larger package size, which may impact load times and bundle sizes
- More complex API, potentially requiring a steeper learning curve
- Higher memory usage due to its extensive feature set
Code Comparison
microdiff:
import { diff } from 'microdiff';
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 3, c: 4 };
console.log(diff(obj1, obj2));
jsdiff:
import { diffJson } from 'diff';
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 3, c: 4 };
console.log(diffJson(obj1, obj2));
Summary
microdiff is a lightweight and focused library for object diffing, while jsdiff offers a more comprehensive set of diffing tools for various use cases. microdiff is ideal for simple object comparisons and projects where minimizing package size is crucial. jsdiff is better suited for complex diffing needs across multiple file types and programming languages, but comes with a larger footprint and potentially more complexity.
fastest deep equal comparison for React
Pros of react-fast-compare
- More mature and widely adopted project with a larger community
- Specifically optimized for React applications and their data structures
- Extensive test suite covering various edge cases
Cons of react-fast-compare
- Larger bundle size compared to microdiff
- Less flexible for non-React use cases
- More complex implementation, potentially harder to maintain
Code Comparison
react-fast-compare:
function equal(a, b) {
if (a === b) return true;
if (a && b && typeof a == 'object' && typeof b == 'object') {
// ... (additional comparison logic)
}
return false;
}
microdiff:
export default function diff(obj1: any, obj2: any): Change[] {
if (obj1 === obj2) return [];
if (typeof obj1 !== typeof obj2) return [{ type: "CHANGE", path: [], value: obj2 }];
// ... (additional comparison logic)
}
Both libraries aim to provide efficient comparison of JavaScript objects, but they have different focuses. react-fast-compare is tailored for React applications and optimizes for their specific use cases, while microdiff offers a more general-purpose solution with a smaller footprint. The choice between them depends on the specific requirements of your project, such as whether you're working with React or need a more lightweight solution for object comparison.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Microdiff is a tiny (currently <1kb), fast, zero dependency object and array comparison library. It is significantly faster than most other deep comparison libraries, and has full TypeScript support.
ð¡ I recommend reading this blog post:
Building the fastest object and array differ for an explanation of how Microdiff achieves its size and speed.
Features
- ð More than double the speed of other object diff libraries
- ð¦ Extremely lightweight, <1kb minified
- ð Supports Deno, Node, the web, and even service workers. Also comes with built-in Typescript types
- ð° Very easy to use, having just a single
diff()
function - ð
Full support for objects like
new Date()
andnew RegExp()
Get started
First, install Microdiff
npm i microdiff
If you are using Deno, you can import it from Deno.land with the link https://deno.land/x/microdiff@VERSION/index.ts
(remember to change @VERSION
to the version you want to use).
After you install it, import it and run it on two objects.
import diff from "microdiff";
const obj1 = {
originalProperty: true,
};
const obj2 = {
originalProperty: true,
newProperty: "new",
};
console.log(diff(obj1, obj2));
// [{type: "CREATE", path: ["newProperty"], value: "new"}]
If you are using CommonJS, you can import it like this:
const diff = require("microdiff").default;
There are three different types of changes: CREATE
, REMOVE
, and CHANGE
.
The path
property gives a path to the property in the new object (or the old object in the case of REMOVE
).
Each element in the paths is a key to the next property a level deeper until you get to the property changed, and it is a string or a number, depending on whether the object is an Array or Object (Objects with number keys will still be strings).
The value
property exists in types CREATE
and CHANGE
, and it contains the value of the property added/changed/deleted.
The oldValue
property exists in the type CHANGE
and REMOVE
, and it contains the old value of the property.
Cycles support
By default, Microdiff supports cyclical references, but if you are sure that the object has no cycles like parsed JSON, you can disable cycles using the cyclesFix
option.
diff(obj1, obj2, { cyclesFix: false });
Benchmarks
Benchmarks: Small object
deep-diff: 17929ns - 409% slower
deep-object-diff: 10763ns - 206% slower
jsdiff: 79700ns - 2164% slower
microdiff: 3520ns - Fastest
Benchmarks: Large Object
deep-diff: 272887ns - 259% slower
deep-object-diff: 160019ns - 111% slower
jsdiff: 1688294ns - 2123% slower
microdiff: 75934ns - Fastest
These benchmarks are currently only for one small object and a very large object, so they might not be accurate. I will be working on creating benchmarks with more varying types.
Contributing
Thanks for helping the project out! Contributing is pretty simple. Fork the repository (if you need more information on how to do this, check out this GitHub guide), clone it to your computer, and start programming! To compile the program, run npm run build
(replace npm
with pnpm
or yarn
if you are using one of those). This will create CommonJS and ESM modules in /dist
.
To benchmark microdiff, you can run npm run bench
. This will automatically build Microdiff and run a benchmarking program comparing microdiff to other common diffing libraries.
Finally, Microdiff has an extensive test suite which you should take advantage of. To make sure everything is working correctly, you can run npm run test
. npm run test
builds the project and then runs the entire test suite on the new version. If you are fixing a bug, be sure to add a test for that.
Also, make sure you read the Code of Conduct before contributing.
Top Related Projects
Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
Diff & patch JavaScript objects
Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.
A javascript text differencing implementation.
fastest deep equal comparison for React
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot