Skip to content

@technobuddha > library > Array > Comparison

Function: changed()

ts
function changed(a: readonly unknown[], b: readonly unknown[]): boolean;

Defined in: changed.ts:37

Determines whether two arrays of dependencies have changed (shallow comparison).

Similar to how React compares dependencies in hooks. Returns true if the arrays are different in length or any element is not strictly equal (===).

Parameters

ParameterTypeDescription
areadonly unknown[]The previous dependency array.
breadonly unknown[]The next dependency array.

Returns

boolean

true if the arrays differ in length or any element, otherwise false.

Examples

typescript
// Basic usage
changed([1, 2, 3], [1, 2, 3]); // false
changed([1, 2, 3], [1, 2, 4]); // true
changed([1, 2], [1, 2, 3]);    // true
changed([], []);               // false
typescript
// In a custom hook
function useCustom(dep1, dep2) {
  const deps = [dep1, dep2];
  const prevDeps = React.useRef(deps);
  React.useEffect(() => {
    if (changed(prevDeps.current, deps)) {
      // dependencies changed, do something
      prevDeps.current = deps;
    }
  }, [dep1, dep2]);
}