Lodash _.matches Deep Comparison Explained

In the Lodash JavaScript library, the _.matches method generates a predicate function designed to perform a deep partial comparison between a target object and a predefined source object. This article breaks down how this deep comparison mechanism operates, how it evaluates nested properties and complex data types, and how its partial matching logic differs from standard full-equality comparisons.

The Generated Predicate Function

When calling _.matches(source), Lodash produces a function with the signature (object) => boolean. This function accepts a single argument—the candidate object—and evaluates whether the candidate conforms to the structure and values specified in the initial source object. It acts as an accessor shorthand commonly used in functional iterators like _.filter, _.find, and _.some.

Deep Partial Comparison Mechanics

The comparison generated by _.matches is governed internally by Lodash’s isMatch algorithm (specifically the internal baseIsMatch function). The operation has two distinct characteristics: it is partial, and it is deep.

Type Handling and Equality Semantics

For value evaluation at each depth of the object tree, the generated function utilizes deep equality semantics equivalent to _.isEqual. This enables robust comparison across diverse JavaScript types:

Code Demonstration

const _ = require('lodash');

const criteria = {
  user: {
    role: 'admin',
    settings: {
      notifications: true
    }
  },
  tags: ['active']
};

// Generates the deep partial comparison function
const isAdminWithNotifications = _.matches(criteria);

const candidateUser = {
  id: 101,
  user: {
    role: 'admin',
    username: 'johndoe',
    settings: {
      notifications: true,
      theme: 'dark'
    }
  },
  tags: ['active', 'verified'] // Note: arrays require exact match by index
};

// Returns false because tags[1] in the candidate does not exist in criteria array
console.log(isAdminWithNotifications(candidateUser)); 

Key Differences from Strict Equality

Unlike native comparison operators (===) or shallow comparison utilities, the function created by _.matches bypasses reference checks in favor of structural validation. It traverses solely the paths defined in the reference object, confirming that every leaf node satisfies deep equality while leaving non-specified branches unexamined.