|
| 1 | +<img align="left" src="https://open-tech-foundation.pages.dev/img/Logo.svg" width="50" height="50"> |
| 2 | + |
| 3 | + [OPEN TECH FOUNDATION](https://open-tech-foundation.pages.dev/) |
| 4 | + |
| 5 | +<div align="center"> |
| 6 | + |
| 7 | +# obj-diff |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +> The Fast, Accurate, JavaScript Objects Diffing Library. |
| 12 | +
|
| 13 | +## Features |
| 14 | + |
| 15 | +- Deep Objects Diffing |
| 16 | + |
| 17 | +- Supports More Object Types, eg: `Map`, `Set` |
| 18 | + |
| 19 | +- TypeScript Support |
| 20 | + |
| 21 | +## Supported Types |
| 22 | + |
| 23 | +- Primitives |
| 24 | + - Undefined |
| 25 | + - Null |
| 26 | + - Number |
| 27 | + - String |
| 28 | + - Boolean |
| 29 | + - BigInt |
| 30 | + |
| 31 | +- Objects |
| 32 | + - Plain Objects, eg: `{}` |
| 33 | + - Array |
| 34 | + - Date |
| 35 | + - Map |
| 36 | + - Set |
| 37 | + |
| 38 | +## Installation |
| 39 | + |
| 40 | +Install it using your favourite package manager. |
| 41 | + |
| 42 | +```sh |
| 43 | +npm install @opentf/obj-diff |
| 44 | +``` |
| 45 | + |
| 46 | +```sh |
| 47 | +yarn add @opentf/obj-diff |
| 48 | +``` |
| 49 | + |
| 50 | +```sh |
| 51 | +pnpm add @opentf/obj-diff |
| 52 | +``` |
| 53 | + |
| 54 | +```sh |
| 55 | +bun add @opentf/obj-diff |
| 56 | +``` |
| 57 | + |
| 58 | +```sh |
| 59 | +deno add @opentf/obj-diff |
| 60 | +``` |
| 61 | + |
| 62 | +## Usage |
| 63 | + |
| 64 | +```js |
| 65 | +import { diff } from '@opentf/obj-diff'; |
| 66 | + |
| 67 | +diff(obj1: object, obj2: object): Array<DiffResult> |
| 68 | +``` |
| 69 | + |
| 70 | +```ts |
| 71 | +type DiffResult = { |
| 72 | + t: 0 | 1 | 2; // The type of diff, 0 - Deleted, 1 - Created, 2 - Updated |
| 73 | + p: Array<string | number>; // The object path |
| 74 | + v?: unknown; // The current value |
| 75 | +}; |
| 76 | +``` |
| 77 | + |
| 78 | +## Examples |
| 79 | + |
| 80 | +1. Diff two simple objects. |
| 81 | + |
| 82 | +```js |
| 83 | +const a = {a: 1, b: 2}; |
| 84 | +const b = {a: 2, c: 3}; |
| 85 | + |
| 86 | +diff(a, b); |
| 87 | +/* |
| 88 | +[ |
| 89 | + { |
| 90 | + t: 2, |
| 91 | + p: ["a"], |
| 92 | + v: 2, |
| 93 | + }, |
| 94 | + { |
| 95 | + t: 0, |
| 96 | + p: ["b"], |
| 97 | + }, |
| 98 | + { |
| 99 | + t: 1, |
| 100 | + p: ["c"], |
| 101 | + v: 5, |
| 102 | + }, |
| 103 | +] |
| 104 | +*/ |
| 105 | +``` |
| 106 | + |
| 107 | +2. Diff two arrays. |
| 108 | + |
| 109 | +```js |
| 110 | +const a = [1, 2, 3, 4, 5]; |
| 111 | +const b = [1, 3, 5]; |
| 112 | + |
| 113 | +diff(a, b); |
| 114 | +/* |
| 115 | +[ |
| 116 | + { |
| 117 | + t: 2, |
| 118 | + p: [1], |
| 119 | + v: 3, |
| 120 | + }, |
| 121 | + { |
| 122 | + t: 2, |
| 123 | + p: [2], |
| 124 | + v: 5, |
| 125 | + }, |
| 126 | + { |
| 127 | + t: 0, |
| 128 | + p: [3], |
| 129 | + }, |
| 130 | + { |
| 131 | + t: 0, |
| 132 | + p: [4], |
| 133 | + }, |
| 134 | +] |
| 135 | +*/ |
| 136 | +``` |
| 137 | + |
| 138 | +3. Deep diff two objects. |
| 139 | + |
| 140 | +```js |
| 141 | +const a = { |
| 142 | + foo: { |
| 143 | + bar: { |
| 144 | + a: ['a', 'b'], |
| 145 | + b: 2, |
| 146 | + c: ['x', 'y'], |
| 147 | + e: 100 |
| 148 | + } |
| 149 | + }, |
| 150 | + buzz: 'world' |
| 151 | +}; |
| 152 | + |
| 153 | +const b = { |
| 154 | + foo: { |
| 155 | + bar: { |
| 156 | + a: ['a'], |
| 157 | + b: 2, |
| 158 | + c: ['x', 'y', 'z'], |
| 159 | + d: 'Hello, world!' |
| 160 | + } |
| 161 | + }, |
| 162 | + buzz: 'fizz' |
| 163 | +}; |
| 164 | + |
| 165 | +diff(a, b); |
| 166 | +/* |
| 167 | +[ |
| 168 | + { |
| 169 | + t: 0, |
| 170 | + p: ["foo", "bar", "a", 1], |
| 171 | + }, |
| 172 | + { |
| 173 | + t: 1, |
| 174 | + p: ["foo", "bar", "c", 2], |
| 175 | + v: "z", |
| 176 | + }, |
| 177 | + { |
| 178 | + t: 0, |
| 179 | + p: ["foo", "bar", "e"], |
| 180 | + }, |
| 181 | + { |
| 182 | + t: 1, |
| 183 | + p: ["foo", "bar", "d"], |
| 184 | + v: "Hello, world!", |
| 185 | + }, |
| 186 | + { |
| 187 | + t: 2, |
| 188 | + p: ["buzz"], |
| 189 | + v: "fizz", |
| 190 | + }, |
| 191 | +] |
| 192 | +*/ |
| 193 | +``` |
| 194 | + |
| 195 | +## Benchmark |
| 196 | + |
| 197 | +```diff |
| 198 | +┌───┬──────────────────┬─────────┬───────────────────┬────────┬─────────┐ |
| 199 | +│ │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │ |
| 200 | +├───┼──────────────────┼─────────┼───────────────────┼────────┼─────────┤ |
| 201 | ++ 0 │ diff │ 252,694 │ 3957.346814404028 │ ±1.60% │ 25270 │ |
| 202 | +│ 1 │ microdiff │ 218,441 │ 4577.892286564301 │ ±0.92% │ 21845 │ |
| 203 | +│ 2 │ deep-object-diff │ 121,385 │ 8238.188318642591 │ ±1.66% │ 12139 │ |
| 204 | +│ 3 │ just-diff │ 105,292 │ 9497.35384615396 │ ±1.66% │ 10530 │ |
| 205 | +│ 4 │ deep-diff │ 160,802 │ 6218.820533549017 │ ±1.59% │ 16081 │ |
| 206 | +└───┴──────────────────┴─────────┴───────────────────┴────────┴─────────┘ |
| 207 | +``` |
| 208 | + |
| 209 | + |
| 210 | +### Running benchmarks |
| 211 | + |
| 212 | +```sh |
| 213 | +$ bun run build |
| 214 | +$ bun benchmark.js |
| 215 | +``` |
| 216 | + |
| 217 | +## Articles |
| 218 | + |
| 219 | +Please read our important articles: |
| 220 | + |
| 221 | +- [Introducing Our New JavaScript Standard Library](https://ganapathy.hashnode.dev/introducing-our-new-javascript-standard-library) |
| 222 | + |
| 223 | +- [You Don’t Need JavaScript Native Methods](https://ganapathy.hashnode.dev/you-dont-need-javascript-native-methods) |
| 224 | + |
| 225 | +## License |
| 226 | + |
| 227 | +Copyright (c) [Thanga Ganapathy](https://github.com/Thanga-Ganapathy) ([MIT License](../../LICENSE)). |
0 commit comments