https://github.com/indutny/sneequals
Sneaky equality check between objects using proxies
https://github.com/indutny/sneequals
Last synced: 7 days ago
JSON representation
Sneaky equality check between objects using proxies
- Host: GitHub
- URL: https://github.com/indutny/sneequals
- Owner: indutny
- License: mit
- Created: 2022-12-23T02:03:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-19T02:51:09.000Z (over 2 years ago)
- Last Synced: 2025-03-29T03:41:31.386Z (25 days ago)
- Language: TypeScript
- Homepage: https://indutny.github.io/sneequals/
- Size: 306 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# @indutny/sneequals
[](https://www.npmjs.com/package/@indutny/sneequals)
[](https://bundlephobia.com/result?p=@indutny/sneequals)
[API docs](https://indutny.github.io/sneequals).
Sneaky equals comparison between objects that checks only the properties that
were touched.Heavily inspired by [proxy-compare](https://github.com/dai-shi/proxy-compare).
## Installation
```sh
npm install @indutny/sneequals
```## Usage
```js
import { watch } from '@indutny/sneequals';const originalData = {
nested: {
prop: 1,
},
avatar: {
src: 'image.png',
},
};const { proxy, watcher } = watch(originalData);
function doSomethingWithData(data) {
return {
prop: data.nested.prop,
x: data.avatar,
};
}const result = watcher.unwrap(doSomethingWithData(proxy));
// Prevent further access to proxy
watcher.stop();const sneakyEqualData = {
nested: {
prop: 1,
other: 'ignored',
},
avatar: original.avatar,
};console.log(watcher.isChanged(originalData, sneakyEqualData)); // false
const sneakyDifferentData = {
nested: {
prop: 2,
},
avatar: {
...original.avatar,
},
};console.log(watcher.isChanged(originalData, sneakyDifferentData)); // true
```## Benchmarks
On M1 Macbook Pro 13:
```sh
% npm run bench -- --duration 60 --ignore-outliers> @indutny/[email protected] bench
> bencher dist/benchmarks/*.jsisChanged: 4’347’490.6 ops/sec (±21’862.0, p=0.001, o=3/100)
isNotChanged: 7’826’035.5 ops/sec (±46’826.6, p=0.001, o=0/100)
memoize: 8’244’416.2 ops/sec (±34’162.8, p=0.001, o=1/100)
watch+unwrap: 729’825.5 ops/sec (±1’403.9, p=0.001, o=5/100)
```## Credits
- Based on [proxy-compare](https://github.com/dai-shi/proxy-compare) by
[dai-shi](https://github.com/dai-shi)
- Name coined by [Scott Nonnenberg](https://github.com/scottnonnenberg/).## LICENSE
This software is licensed under the MIT License.