Compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity

When comparing objects in JavaScript, we often need to determine their similarity as a percentage. This is useful for data matching, filtering, or recommendation systems.

Problem Overview

Given two objects, we need to calculate their similarity percentage based on matching key-value pairs. The similarity is calculated by dividing the count of matching properties by the total properties in the smaller object.

const a = {
    Make: "Apple",
    Model: "iPad",
    hasScreen: "yes",
    Review: "Great product!",
};

const b = {
    Make: "Apple",
    Model: "iPad",
    waterResistant: false
};

console.log("Object A:", a);
console.log("Object B:", b);
Object A: { Make: 'Apple', Model: 'iPad', hasScreen: 'yes', Review: 'Great product!' }
Object B: { Make: 'Apple', Model: 'iPad', waterResistant: false }

Algorithm

The calculation works as follows:

  1. Identify the smaller object (fewer properties)
  2. Count matching key-value pairs between objects
  3. Calculate percentage: (matches / smaller object size) × 100

Implementation

const findSimilarity = (first, second) => {
    const firstLength = Object.keys(first).length;
    const secondLength = Object.keys(second).length;
    const smaller = firstLength < secondLength ? first : second;
    const greater = smaller === first ? second : first;
    
    const count = Object.keys(smaller).reduce((acc, val) => {
        if (Object.keys(greater).includes(val)) {
            if (greater[val] === smaller[val]) {
                return ++acc;
            }
        }
        return acc;
    }, 0);
    
    return (count / Math.min(firstLength, secondLength)) * 100;
};

// Test with our objects
const a = {
    Make: "Apple",
    Model: "iPad",
    hasScreen: "yes",
    Review: "Great product!",
};

const b = {
    Make: "Apple",
    Model: "iPad",
    waterResistant: false
};

console.log("Similarity percentage:", findSimilarity(a, b));
Similarity percentage: 66.66666666666666

How It Works

In this example:

  • Object a has 4 properties, object b has 3 properties
  • The smaller object b is used as the base (3 properties)
  • Matching pairs: "Make: Apple" and "Model: iPad" (2 matches)
  • Similarity: (2 ÷ 3) × 100 = 66.67%

Enhanced Version with Rounded Result

const findSimilarityRounded = (first, second, decimals = 2) => {
    const firstLength = Object.keys(first).length;
    const secondLength = Object.keys(second).length;
    const smaller = firstLength < secondLength ? first : second;
    const greater = smaller === first ? second : first;
    
    const count = Object.keys(smaller).reduce((acc, val) => {
        if (greater.hasOwnProperty(val) && greater[val] === smaller[val]) {
            return acc + 1;
        }
        return acc;
    }, 0);
    
    const percentage = (count / Math.min(firstLength, secondLength)) * 100;
    return Math.round(percentage * Math.pow(10, decimals)) / Math.pow(10, decimals);
};

console.log("Rounded similarity:", findSimilarityRounded(a, b));
Rounded similarity: 66.67

Edge Cases

// Empty objects
const empty1 = {};
const empty2 = {};
console.log("Empty objects:", findSimilarity(empty1, empty2));

// Identical objects
const identical1 = {x: 1, y: 2};
const identical2 = {x: 1, y: 2};
console.log("Identical objects:", findSimilarity(identical1, identical2));

// No matches
const noMatch1 = {a: 1, b: 2};
const noMatch2 = {c: 3, d: 4};
console.log("No matches:", findSimilarity(noMatch1, noMatch2));
Empty objects: NaN
Identical objects: 100
No matches: 0

Conclusion

This object comparison function calculates similarity percentages by counting matching key-value pairs. It's useful for data analysis, content matching, and building recommendation systems where object similarity matters.

Updated on: 2026-03-15T23:18:59+05:30

639 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements