# @asmartbear/pojo-compare

> Compares two POJOs, either for equality or for less/equal/greater

Latest version **1.0.2** (published 2020-05-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @asmartbear/pojo-compare
pnpm add @asmartbear/pojo-compare
yarn add @asmartbear/pojo-compare
bun add @asmartbear/pojo-compare
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-05-02 |
| First published | 2020-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jason Cohen |
| Maintainers | asmartbear |
| Keywords | diff, compare, pojo, json |

## Links

- npm: https://www.npmjs.com/package/@asmartbear/pojo-compare
- Repository: https://github.com/asmartbear/pojo-compare
- Homepage: https://github.com/asmartbear/pojo-compare#readme
- Issues: https://github.com/asmartbear/pojo-compare/issues
- npm.io page: https://npm.io/package/@asmartbear/pojo-compare

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-05-02
- 1.0.1 — 2020-04-30
- 1.0.0 — 2020-04-12

## README

# POJO Compare

Extremely fast deep-comparison function (both "equal" and "greater/equal/lesser") for POJOs that are compatible with JSON-encoding, meaning for types matching this Typescript definition:

```typescript
type POJO = null|boolean|number|string|Array<POJO>|{ [field:string]: POJO };
```

Uses:

* Like "Deep Equal," but additional option for total ordering.
* In `Array#sort()` to sort POJOs quickly.

Features:

* **Fast:** Faster than other npm libraries for deep-equals (see below), due to only caring about POJOs.
* **Small:** No dependencies, extremely small amount of code.
* **Typescript:** Written in Typescript, but distributed as regular Javascript, and includes Typescript declarations.

## Usage

Use through [`npm`](https://www.npmjs.com/package/@asmartbear/pojo-compare), or get the source from [Github](https://github.com/asmartbear/pojo-compare).

### `equal()`

```javascript
import { equal as eq } from '@asmartbear/pojo-compare';

console.log(eq(null,null));            // -> true
console.log(eq(null,undefined));       // -> false
console.log(eq(123,"123"));            // -> false   (type-specific!)
console.log(eq([1,"2"],[1,"2"]]));     // -> true
console.log(eq({a:1,b:2}, {b:2,a:1})); // -> true
```

### `compare()`

Returns `-1` if lesser, `1` if greater, or `0` if equal.  If you supply non-POJO objects, the results are undefined.

```javascript
import { compare as cmp } from '@asmartbear/pojo-compare';

console.log(cmp(null,null));  // -> 0
console.log(cmp(null,undefined)); // -> 1  (null > undefined as a convention)
console.log(cmp(123,321));    // -> -1
console.log(cmp(123,"1"));    // -> -1     (numbers always less than strings)
console.log(cmp("21","1"));   // -> 1      (strings compared as strings)
console.log(cmp([],[]]));     // -> 0
console.log(cmp([],{}));      // -> -1
console.log(cmp([1,5,5],[99,5,5])); // -> -1   (arrays compare in order)
console.log(cmp(
    {a: 1, b: 2},
    {a: 2, b: 1}
)); // -> -1   (maps compare keys in their sorted order)
console.log(cmp(
    {a: 2},
    {b: 1}
)); // -> -1   (maps keys come first; here a < b)
```

## Benchmarks

The benchmarks are included in the project if you want to run them youself, or please contribute new ones!

This `equal` is the fastest; `compare` is slower due to fewer opportunities for short-circuiting the answer.

```
pojo-compare x 11,120 ops/sec ±1.43% (91 runs sampled)
pojo-equal x 18,642 ops/sec ±0.81% (95 runs sampled)
qcompare x 17,547 ops/sec ±1.34% (89 runs sampled)
fast-deep-equal x 15,459 ops/sec ±1.38% (86 runs sampled)
fast-equals x 13,019 ops/sec ±1.43% (90 runs sampled)
nano-equal x 11,296 ops/sec ±1.31% (87 runs sampled)
deep-equal x 11.37 ops/sec ±1.79% (33 runs sampled)
deep-eql x 4,550 ops/sec ±1.03% (91 runs sampled)
Fastest is pojo-equal
```

---
_Source: https://npm.io/package/@asmartbear/pojo-compare · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
