npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

object-diff-ts

v1.0.1

Published

A high-performance object diffing library for MongoDB-like objects

Readme

object-diff-ts

CI npm version License: ISC TypeScript

A high-performance object diffing library for MongoDB-like objects. This library compares two objects and returns detailed information about additions, deletions, and updates.

Features

  • High Performance: Optimized for speed, handling up to 50,000 calls with objects containing 50-60 properties
  • Deep Nesting: Supports up to 10 levels of nested object comparison (configurable)
  • Type Coercion: Handles type differences like "1" vs 1, true vs "true", Date vs string
  • Flexible Ignoring: Ignore specific properties or use wildcards (e.g., user._*)
  • Array Support: Configurable array order sensitivity
  • TypeScript: Full TypeScript support with comprehensive type definitions
  • ES Modules: Modern ES module support

Installation

npm install object-diff-ts

Quick Start

import { diff } from 'object-diff-ts';

const objectA = {
  name: 'John',
  age: 30,
  profile: {
    email: '[email protected]',
    active: true,
  },
};

const objectB = {
  name: 'John',
  age: 31,
  profile: {
    email: '[email protected]',
    active: 'true',
    phone: '+1234567890',
  },
};

const result = diff(objectA, objectB);

console.log(result);
// Output:
// {
//   additions: {
//     profile: { phone: '+1234567890' }
//   },
//   deletions: {},
//   updates: {
//     age: { from: 30, to: 31 },
//     profile: { active: { from: true, to: 'true' } }
//   }
// }

API Reference

diff(objectA, objectB, options?)

Compares two objects and returns the differences.

Parameters

  • objectA (any): The first object to compare
  • objectB (any): The second object to compare
  • options (DiffOptions, optional): Configuration options

Returns

  • DiffResult: Object containing additions, deletions, and updates

DiffOptions

interface DiffOptions {
  ignoreProperties?: string[]; // Properties to ignore during comparison
  enableTypeCoercion?: boolean; // Enable type coercion (default: true)
  arrayOrderMatters?: boolean; // Whether array order matters (default: true)
  maxDepth?: number; // Maximum nesting depth (default: 10)
}

DiffResult

interface DiffResult {
  additions: Record<string, any>; // Properties in B but not in A
  deletions: Record<string, any>; // Properties in A but not in B
  updates: Record<string, any>; // Properties in both but with different values
}

Usage Examples

Basic Comparison

import { diff } from 'object-diff-ts';

const result = diff(
  { name: 'John', age: 30 },
  { name: 'John', age: 31, city: 'NYC' }
);

// result.additions = { city: 'NYC' }
// result.deletions = {}
// result.updates = { age: { from: 30, to: 31 } }

Ignoring Properties

const result = diff(objectA, objectB, {
  ignoreProperties: ['_id', 'createdAt', 'user._*'],
});

Disabling Type Coercion

const result = diff(
  { age: 30, active: true },
  { age: '30', active: 'true' },
  { enableTypeCoercion: false }
);

// Will detect differences due to type mismatch

Array Order Sensitivity

// Order matters (default)
const result1 = diff({ tags: ['js', 'ts'] }, { tags: ['ts', 'js'] });
// result1.updates = { tags: { from: ['js', 'ts'], to: ['ts', 'js'] } }

// Order doesn't matter
const result2 = diff(
  { tags: ['js', 'ts'] },
  { tags: ['ts', 'js'] },
  { arrayOrderMatters: false }
);
// result2.updates = {} (no difference detected)

Nested Object Comparison

const result = diff(
  {
    user: {
      name: 'John',
      profile: { email: '[email protected]' },
    },
  },
  {
    user: {
      name: 'John',
      profile: {
        email: '[email protected]',
        phone: '+1234567890',
      },
    },
  }
);

// result.additions = {
//   user: { profile: { phone: '+1234567890' } }
// }

Type Coercion Examples

// String vs Number
diff({ age: 30 }, { age: '30' });
// No differences detected (type coercion enabled)

// Boolean vs String
diff({ active: true }, { active: 'true' });
// No differences detected

// Date vs String
const date = new Date('2023-01-01');
diff({ createdAt: date }, { createdAt: '2023-01-01T00:00:00.000Z' });
// No differences detected

Performance Considerations

  • Speed Optimized: Designed for high-throughput scenarios
  • Memory Efficient: Minimal object creation during comparison
  • Depth Limiting: Configurable max depth prevents infinite recursion
  • Early Exit: Stops comparison as soon as differences are found

Advanced Usage

Custom Property Ignoring

// Ignore specific properties
const result = diff(objA, objB, {
  ignoreProperties: ['_id', 'version', 'metadata'],
});

// Ignore nested properties
const result = diff(objA, objB, {
  ignoreProperties: ['user._id', 'user.profile._*'],
});

// Use wildcards
const result = diff(objA, objB, {
  ignoreProperties: ['*._id', '*.createdAt'],
});

Handling Large Objects

// For objects with many properties, consider ignoring frequently changing fields
const result = diff(largeObjectA, largeObjectB, {
  ignoreProperties: ['lastModified', 'checksum', 'cache.*'],
  maxDepth: 3, // Limit depth for performance
});

Development

Building

npm run build

Testing

npm test
npm run test:watch

Linting

npm run lint
npm run lint:fix

Formatting

npm run format

License

ISC

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
  6. Submit a pull request