Deep Value Equality Comparison
The `value-equal` package provides a lightweight utility function designed to determine if two JavaScript values are deeply equal based on their content, rather than strict reference equality. It intelligently compares primitive values, arrays, and plain objects by recursively checking their `valueOf` representations. Currently at stable version 1.0.1 (published 7 years ago), the package demonstrates a mature and reliable API. Its release cadence is likely infrequent due to its focused scope and stable implementation, with updates typically limited to critical bug fixes. This package serves as a practical alternative for scenarios where deep value comparison is essential, such as normalizing data for `localStorage` or comparing `window.history.state` values, where objects need to be treated as equal if their contents match. Unlike strict equality (`===`) or more feature-rich libraries, `value-equal` offers a streamlined approach specifically for these use cases without additional overhead.
Common errors
-
TypeError: (0 , value_equal__WEBPACK_IMPORTED_MODULE_0__.default) is not a function
cause Incorrect ES module import syntax for a default export. This error typically occurs when attempting to use a named import (`import { valueEqual } from 'value-equal'`) for a module that only provides a default export.fixUse the correct default import syntax: `import valueEqual from 'value-equal';` -
ReferenceError: valueEqual is not defined
cause The `valueEqual` function was not properly imported or made available in the current JavaScript scope. This can happen if the module is not imported at all, or if the UMD build is used in a browser without accessing it via `window.valueEqual`.fixEnsure the module is imported correctly using `import valueEqual from 'value-equal';` (ESM) or `const valueEqual = require('value-equal');` (CommonJS). If using the UMD build in a browser, ensure the script tag is present and you are accessing it via `window.valueEqual`.
Warnings
- gotcha This library performs deep value equality, meaning it compares the contents of objects and arrays recursively. It does *not* perform strict reference equality (`obj1 === obj2`). Users should be aware that `value-equal` may not handle circular references gracefully, potentially leading to infinite loops or stack overflows on extremely deep or interconnected structures, as this is not explicitly addressed in its API.
- gotcha The package is primarily designed for primitive values, plain JavaScript objects, and arrays. Its comparison relies on `Object.prototype.valueOf` and direct property iteration, which might not yield expected results for custom class instances, built-in complex objects like `Map`, `Set`, `Date`, `RegExp`, or instances with non-enumerable properties. For example, `Date` objects with the same time value may be considered unequal if their internal representations differ, or if `valueOf` returns a primitive but other properties are ignored.
- deprecated This package (v1.0.1) has not been updated since its last publish 7 years ago, indicating it is in maintenance mode or potentially unmaintained. While its core functionality remains valid for its intended simple use cases, it might not receive updates for new JavaScript features, performance optimizations, or potential security vulnerabilities discovered in its (currently zero) dependencies or underlying JavaScript runtime. The `README.md` itself was last updated in 2017.
Install
-
npm install value-equal -
yarn add value-equal -
pnpm add value-equal
Imports
- valueEqual
import { valueEqual } from 'value-equal';import valueEqual from 'value-equal';
- valueEqual
const { valueEqual } = require('value-equal');const valueEqual = require('value-equal'); - valueEqual
console.log(window.valueEqual(1, 1));
Quickstart
import valueEqual from 'value-equal';
// Primitive values
console.log('Primitives:');
console.log(valueEqual(1, 1)); // true
console.log(valueEqual('hello', 'hello')); // true
console.log(valueEqual(true, false)); // false
console.log(valueEqual(null, null)); // true
console.log(valueEqual(undefined, undefined)); // true
// String objects vs primitives
console.log('\nStrings:');
console.log(valueEqual('asdf', new String('asdf'))); // true
// Plain objects
console.log('\nObjects:');
console.log(valueEqual({ a: 'a', b: 1 }, { a: 'a', b: 1 })); // true
console.log(valueEqual({ a: 'a' }, { a: 'b' })); // false
console.log(valueEqual({ c: 3, a: 1 }, { a: 1, c: 3 })); // true (order of keys doesn't matter for deep equality)
// Arrays
console.log('\nArrays:');
console.log(valueEqual([1, 2, 3], [1, 2, 3])); // true
console.log(valueEqual([1, 2, { a: 1 }], [1, 2, { a: 1 }])); // true (nested objects)
console.log(valueEqual([1, 2, 3], [2, 3, 4])); // false
// Mixed types
console.log('\nMixed Types:');
console.log(valueEqual({ a: 1 }, [1])); // false