Shallow Equality Check

3.1.0 · active · verified Sun Apr 19

The `shallow-equal` library provides minimalistic, TypeScript-compatible utilities for performing shallow equality checks on arrays and objects. It currently stands at version 3.1.0, offering highly optimized functions like `shallowEqualArrays` and `shallowEqualObjects` for specific data types, alongside a generic `shallowEqual` function that includes runtime type checking. The project emphasizes being super light with no external dependencies, ensuring a small footprint. Its release cadence is stable, focusing on minor enhancements and maintenance rather than frequent, large-scale breaking changes, typical for foundational utility packages. A key differentiator is its explicit separation of array and object equality functions, allowing developers to choose the most performant option when the type is known, avoiding the overhead of runtime type inference inherent in more generic solutions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `shallowEqualArrays` and `shallowEqualObjects` for efficient shallow comparisons, clarifying that nested non-primitives are compared by reference, not recursively.

import { shallowEqualArrays, shallowEqualObjects } from "shallow-equal";

// Demonstrates shallow equality for arrays. Nested objects are compared by reference.
const arr1 = [1, 2, { id: 1 }];
const arr2 = [1, 2, { id: 1 }];
console.log("shallowEqualArrays(arr1, arr2):", shallowEqualArrays(arr1, arr2)); // Expected: false (because {id:1} !== {id:1})

const arr3 = [1, 2, 3];
const arr4 = [1, 2, 3];
console.log("shallowEqualArrays(arr3, arr4):", shallowEqualArrays(arr3, arr4)); // Expected: true

// Demonstrates shallow equality for objects. Nested objects are compared by reference.
const obj1 = { a: 5, b: "abc", c: { id: 1 } };
const obj2 = { a: 5, b: "abc", c: { id: 1 } };
console.log("shallowEqualObjects(obj1, obj2):", shallowEqualObjects(obj1, obj2)); // Expected: false (because {id:1} !== {id:1})

const obj3 = { a: 5, b: "abc" };
const obj4 = { a: 5, b: "abc" };
console.log("shallowEqualObjects(obj3, obj4):", shallowEqualObjects(obj3, obj4)); // Expected: true

view raw JSON →