@blazediff/object

中文文档
1.8.0发布于 2026/04/07MITTypeScriptESM + CJS零依赖近 30 天下载 5876

Blazing-fast object comparison

原始使用文档
文档目录9 节
  1. Features
  2. Installation
  3. Quick Start
  4. API Reference
  5. diff(oldObj, newObj, options?)
  6. Difference Types
  7. Usage
  8. Nested Objects
  9. Array Comparison

@blazediff/object

npm bundle size NPM Downloads

Lightning-fast structural object comparison with detailed change tracking.

Features

  • High Performance: Optimized algorithm with intelligent key lookup strategies for large objects
  • Precise Tracking: Detailed path tracking for nested object modifications
  • Comprehensive Types: Handles primitives, objects, arrays, dates, regex, and circular references
  • Memory Efficient: Minimal overhead with consistent object shapes for V8 optimization
  • Cycle Detection: Built-in circular reference handling with configurable detection
  • Rich Output: Detailed difference objects with type, path, value, and oldValue information

Installation

npm install @blazediff/object

Quick Start

import diff from '@blazediff/object';

const oldObj = {
  name: "John",
  age: 30,
  city: "NYC",
  skills: ["JavaScript", "TypeScript"]
};

const newObj = {
  name: "John",
  age: 31,
  city: "San Francisco",
  skills: ["JavaScript", "TypeScript", "Go"],
  active: true
};

const changes = diff(oldObj, newObj);
console.log(changes);

Output:

[
  {
    "type": 2,
    "path": ["age"],
    "value": 31,
    "oldValue": 30
  },
  {
    "type": 2,
    "path": ["city"],
    "value": "San Francisco",
    "oldValue": "NYC"
  },
  {
    "type": 0,
    "path": ["skills", 2],
    "value": "Go",
    "oldValue": undefined
  },
  {
    "type": 0,
    "path": ["active"],
    "value": true,
    "oldValue": undefined
  }
]

API Reference

diff(oldObj, newObj, options?)

Compares two objects and returns an array of differences.

Parameters:

  • oldObj (any): The original object to compare from
  • newObj (any): The new object to compare to
  • options (object, optional): Configuration options
    • detectCycles (boolean): Enable circular reference detection (default: true)

Returns:

  • Difference[]: Array of difference objects

Difference Types

The diff function returns an array of difference objects with the following structure:

interface Difference {
  type: DifferenceType;
  path: (string | number)[];
  value: any;
  oldValue: any;
}

enum DifferenceType {
  CREATE = 0,  // Property or array element was added
  REMOVE = 1,  // Property or array element was deleted
  CHANGE = 2   // Property or array element value was modified
}

Difference Types:

  • 0 (CREATE) - Property or array element was added
  • 1 (REMOVE) - Property or array element was deleted
  • 2 (CHANGE) - Property or array element value was modified

All difference objects maintain consistent shape with type, path, value, and oldValue fields for optimal V8 performance.

Usage

import diff from '@blazediff/object';

// Simple property changes
const result = diff(
  { a: 1, b: 2, c: 3 },
  { a: 1, b: 20, d: 4 }
);
// Returns changes for modified 'b', removed 'c', and added 'd'

Nested Objects

// Deep nested changes
const oldData = {
  user: {
    profile: {
      settings: { theme: 'dark', notifications: true }
    }
  }
};

const newData = {
  user: {
    profile: {
      settings: { theme: 'light', notifications: true }
    }
  }
};

const changes = diff(oldData, newData);
// Returns: [{ type: 2, path: ['user', 'profile', 'settings', 'theme'], value: 'light', oldValue: 'dark' }]

Array Comparison

// Array modifications
const changes = diff(
  { items: ['a', 'b', 'c'] },
  { items: ['a', 'x', 'c', 'd'] }
);
// Detects element changes and additions

版本与发布记录

@blazediff/object 共发布 10 个版本,首次发布于 2025/09/26;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.8.0latest2026/04/077 个月前
1.7.12026/01/129 个月前
1.7.02025/11/2211 个月前
1.6.02025/10/211 年前
1.5.02025/10/171.1 年前
1.4.12025/10/091.1 年前

关于 @blazediff/object 的常见问题

@blazediff/object 是什么?

Blazing-fast object comparison

如何安装 @blazediff/object?

在项目目录运行 npm install @blazediff/object,或使用 pnpm add / yarn add / bun add。

@blazediff/object 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@blazediff/object 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@blazediff/object 可以商用吗?

@blazediff/object 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blazediff/object 的下载量有多少?

根据 npm 官方下载统计,@blazediff/object 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,876 次,日均约 196 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blazediff/object 还在维护吗?

最新版本 1.8.0 发布于 2026/04/07(7 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/09/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。