Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
shallow-equal
Advanced tools
Typescript-compatible minimalistic shallow equality check for arrays/objects
The shallow-equal npm package is primarily used to perform shallow equality checks on objects and arrays. This means it checks if two objects or arrays have the same values at the top level, without deeply checking nested objects or arrays. It is commonly used in React and other frameworks to optimize rendering by preventing unnecessary updates when data has not effectively changed.
Shallow equality check for objects
This feature allows you to compare two objects to determine if they have the same top-level properties with equal values.
const shallowEqual = require('shallow-equal/objects');
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 2 };
console.log(shallowEqual(obj1, obj2)); // true
Shallow equality check for arrays
This feature enables the comparison of two arrays to check if they contain the same elements in the same order, without considering nested arrays or objects.
const shallowEqual = require('shallow-equal/arrays');
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
console.log(shallowEqual(arr1, arr2)); // true
This package offers similar functionality to shallow-equal, providing a simple and efficient way to compare objects and arrays shallowly. It is often used in similar contexts for optimizing performance in UI frameworks.
Although primarily designed for use with React, react-fast-compare offers optimized deep comparison functions that can be more comprehensive than shallow-equal when deeper object structures need to be compared. It is more suitable when nested data structures are involved but is generally slower for shallow comparisons.
If you know you have two arrays or two objects in hand, and you want to know if they are shallowly equal or not, this library is for you.
npm install shallow-equal --save
or
yarn add shallow-equal
import { shallowEqualArrays } from "shallow-equal";
shallowEqualArrays([1, 2, 3], [1, 2, 3]); // => true
shallowEqualArrays([{ a: 5 }], [{ a: 5 }]); // => false
import { shallowEqualObjects } from "shallow-equal";
shallowEqualObjects({ a: 5, b: "abc" }, { a: 5, b: "abc" }); // => true
shallowEqualObjects({ a: 5, b: {} }, { a: 5, b: {} }); // => false
You can also use the generic form, shallowEqual
. But note that it does runtime type checking in order to decide whether it's comparing arrays or objects, so the convenience comes with a runtime penalty.
import { shallowEqual } from "shallow-equal";
shallowEqual([1, 2, 3], [1, 2, 3]); // => true
shallowEqual({ a: 5, b: {} }, { a: 5, b: {} }); // => false
FAQs
Typescript-compatible minimalistic shallow equality check for arrays/objects
The npm package shallow-equal receives a total of 1,662,324 weekly downloads. As such, shallow-equal popularity was classified as popular.
We found that shallow-equal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.