Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A fast, zero dependency object and array comparison library. Significantly faster than most other deep comparison libraries and has full TypeScript support.

License

NotificationsYou must be signed in to change notification settings

AsyncBanana/microdiff

Repository files navigation

Microdiff Logo

Microdiff is a tiny (<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.

Minizipped Size (from Bundlephobia)Licensedependency Count

Features

  • 🚀 More than double the speed of other object diff libraries
  • 📦 Extremely lightweight, <1kb minified
  • 🌎 Supports Deno, Node, Bun, the web, and even service workers. Also comes with built-in Typescript types
  • 🔰 Very easy to use, having just a singlediff() function
  • 📅 Full support for objects likenew 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 linkhttps://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.

importdifffrom"microdiff";constobj1={originalProperty:true,};constobj2={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:

constdiff=require("microdiff").default;

There are three different types of changes:CREATE,REMOVE, andCHANGE.Thepath property gives a path to the property in the new object (or the old object in the case ofREMOVE).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).Thevalue property exists in typesCREATE andCHANGE, and it contains the value of the property added/changed/deleted.TheoldValue property exists in the typeCHANGE andREMOVE, 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 thecyclesFix option.

diff(obj1,obj2,{cyclesFix:false});

Benchmarks

Geometric mean of time per operation relative to Microdiff (no cycles) (100%==equal time, lower is better)microdiff (no cycles): 100%microdiff: 149%deep-diff: 197%deep-object-diff: 288%jsDiff: 1565%

These results are from a suite of benchmarks matching real world use cases of multiple open-source repos using various diffing algorithm, running under Node 22.12.0 on a Ryzen 7950x clocked at ~4.30 GHz. The benchmarks are run throughmitata to minimize random variation and time most accurately. You can view the full benchmark code inbench.js and the benchmarks themselves atbenchmarks/applied.

Of course,these benchmarks should be taken with a grain of salt due to the inherent errors present in benchmarking JavaScript, but if you want to run them on your own computer in your own runtime/setup, runbench.js.

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 outthis GitHub guide), clone it to your computer, and start programming! To compile the program, runnpm run build (replacenpm withpnpm oryarn if you are using one of those). This will create CommonJS and ESM modules in/dist.

To benchmark microdiff, you can runnpm 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 runnpm 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 theCode of Conduct before contributing.

About

A fast, zero dependency object and array comparison library. Significantly faster than most other deep comparison libraries and has full TypeScript support.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp