{"id":12017,"library":"shallow-equal","title":"Shallow Equality Check","description":"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.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/moroshko/shallow-equal","tags":["javascript","shallow","equality","compare","comparison","shallowequal","shallow-equal","shallowequals","shallow-equals","typescript"],"install":[{"cmd":"npm install shallow-equal","lang":"bash","label":"npm"},{"cmd":"yarn add shallow-equal","lang":"bash","label":"yarn"},{"cmd":"pnpm add shallow-equal","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primarily designed for ESM usage. CommonJS `require` can lead to bundling or runtime issues in pure ESM environments.","wrong":"const { shallowEqualArrays } = require('shallow-equal');","symbol":"shallowEqualArrays","correct":"import { shallowEqualArrays } from 'shallow-equal';"},{"note":"Ensure your environment supports ESM imports. Direct CommonJS access might work but is not the recommended pattern.","wrong":"const shallowEqualObjects = require('shallow-equal').shallowEqualObjects;","symbol":"shallowEqualObjects","correct":"import { shallowEqualObjects } from 'shallow-equal';"},{"note":"`shallowEqual` is a named export, not a default export. Using the generic `shallowEqual` function introduces a small runtime performance penalty due to internal type checking compared to `shallowEqualArrays` or `shallowEqualObjects`.","wrong":"import shallowEqual from 'shallow-equal';","symbol":"shallowEqual","correct":"import { shallowEqual } from 'shallow-equal';"}],"quickstart":{"code":"import { shallowEqualArrays, shallowEqualObjects } from \"shallow-equal\";\n\n// Demonstrates shallow equality for arrays. Nested objects are compared by reference.\nconst arr1 = [1, 2, { id: 1 }];\nconst arr2 = [1, 2, { id: 1 }];\nconsole.log(\"shallowEqualArrays(arr1, arr2):\", shallowEqualArrays(arr1, arr2)); // Expected: false (because {id:1} !== {id:1})\n\nconst arr3 = [1, 2, 3];\nconst arr4 = [1, 2, 3];\nconsole.log(\"shallowEqualArrays(arr3, arr4):\", shallowEqualArrays(arr3, arr4)); // Expected: true\n\n// Demonstrates shallow equality for objects. Nested objects are compared by reference.\nconst obj1 = { a: 5, b: \"abc\", c: { id: 1 } };\nconst obj2 = { a: 5, b: \"abc\", c: { id: 1 } };\nconsole.log(\"shallowEqualObjects(obj1, obj2):\", shallowEqualObjects(obj1, obj2)); // Expected: false (because {id:1} !== {id:1})\n\nconst obj3 = { a: 5, b: \"abc\" };\nconst obj4 = { a: 5, b: \"abc\" };\nconsole.log(\"shallowEqualObjects(obj3, obj4):\", shallowEqualObjects(obj3, obj4)); // Expected: true","lang":"typescript","description":"Demonstrates how to use `shallowEqualArrays` and `shallowEqualObjects` for efficient shallow comparisons, clarifying that nested non-primitives are compared by reference, not recursively."},"warnings":[{"fix":"Use `shallowEqualArrays` for arrays and `shallowEqualObjects` for objects when the input type is statically known.","message":"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`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Understand the distinction between shallow and deep equality. For deep comparisons, you will need a different library (e.g., `lodash.isequal` or a custom recursive comparison).","message":"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})`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your project's `tsconfig.json` `module` and `moduleResolution` settings align with your runtime environment (e.g., `\"module\": \"esnext\", \"moduleResolution\": \"bundler\"` for modern setups) and consistently use `import` statements.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure 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;`.","cause":"Attempting to destructure a CommonJS `require`'s default export when the library provides named ESM exports, or incorrect module interoperability configuration.","error":"TypeError: shallowEqualArrays is not a function"},{"fix":"Use 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.","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.","error":"Property 'shallowEqualObjects' does not exist on type 'typeof import(\"shallow-equal\")'."}],"ecosystem":"npm"}