@blazediff/object
Lightning-fast structural object comparison with detailed change tracking. High-performance algorithm optimized for real-world data structures.
Installation
npm install @blazediff/object
Features
- High Performance - Optimized algorithm with intelligent key lookup strategies
- 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
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
Parameter | Type | Description |
---|---|---|
oldObj | any | The original object to compare from |
newObj | any | The new object to compare to |
options | object | Configuration options (optional) |
Options
Option | Type | Default | Description |
---|---|---|---|
detectCycles | boolean | true | Enable circular reference detection |
Returns
Returns Difference[]
- Array of difference objects with consistent structure:
interface Difference {
type: DifferenceType;
path: (string | number)[];
value: any;
oldValue: any;
}
Difference Types
Difference Types are represented as numbers for optimal performance:
Type | Name | Description |
---|---|---|
0 | CREATE | Property or array element was added |
1 | REMOVE | Property or array element was deleted |
2 | CHANGE | Property or array element was modified |
All difference objects maintain consistent shape with type
, path
, value
, and oldValue
fields for optimal V8 performance.
Links
Last updated on