Shallow Equality Check
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
-
TypeError: shallowEqualArrays is not a function
cause Attempting to destructure a CommonJS `require`'s default export when the library provides named ESM exports, or incorrect module interoperability configuration.fixEnsure you are using `import { shallowEqualArrays } from 'shallow-equal';` for ESM. If strictly using CommonJS, check if the package provides a CJS entry point that you can `require('shallow-equal').shallowEqualArrays;`. -
Property 'shallowEqualObjects' does not exist on type 'typeof import("shallow-equal")'.cause This TypeScript error indicates that the type checker cannot find the named export, often due to an incorrect import statement (e.g., `import * as shallowEqual from 'shallow-equal';` then trying `shallowEqual.shallowEqualObjects`) or mismatched `tsconfig.json` module settings.fixUse a named import: `import { shallowEqualObjects } from 'shallow-equal';`. Verify your `tsconfig.json`'s `module` and `moduleResolution` settings are compatible with how your bundler or Node.js resolves modules.
Warnings
- gotcha The generic `shallowEqual` function performs runtime type checking (to determine if inputs are arrays or objects), which introduces a slight performance overhead. For maximum performance when the type is known, prefer `shallowEqualArrays` or `shallowEqualObjects`.
- gotcha This library performs *shallow* equality checks. This means that for arrays or objects containing other objects or arrays, only the references of the nested structures are compared, not their contents recursively. For example, `shallowEqualArrays([{a:1}], [{a:1}])` will return `false` because `({a:1}) !== ({a:1})`.
- gotcha While the package provides TypeScript types, incorrect import syntax (e.g., using CommonJS `require` in a TypeScript project configured for ESM) can lead to type inference issues or runtime errors, particularly in modern Node.js or bundler setups.
Install
-
npm install shallow-equal -
yarn add shallow-equal -
pnpm add shallow-equal
Imports
- shallowEqualArrays
const { shallowEqualArrays } = require('shallow-equal');import { shallowEqualArrays } from 'shallow-equal'; - shallowEqualObjects
const shallowEqualObjects = require('shallow-equal').shallowEqualObjects;import { shallowEqualObjects } from 'shallow-equal'; - shallowEqual
import shallowEqual from 'shallow-equal';
import { shallowEqual } from 'shallow-equal';
Quickstart
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