SkillAgentSearch skills...

Sneequals

Sneaky equality check between objects using proxies

Install / Use

/learn @indutny/Sneequals
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

@indutny/sneequals

npm size CI Status

API docs.

Sneaky equals comparison between objects that checks only the properties that were touched.

Heavily inspired by proxy-compare.

Installation

npm install @indutny/sneequals

Usage

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:

% npm run bench -- --duration 60 --ignore-outliers

> @indutny/sneequals@1.3.5 bench
> bencher dist/benchmarks/*.js

isChanged:    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

LICENSE

This software is licensed under the MIT License.

View on GitHub
GitHub Stars13
CategoryDevelopment
Updated3y ago
Forks0

Languages

TypeScript

Security Score

75/100

Audited on Jan 31, 2023

No findings