Skip to content

Commit 938bfcd

Browse files
Added browser support & tests. Refactored export.
1 parent 4281577 commit 938bfcd

File tree

8 files changed

+2903
-207
lines changed

8 files changed

+2903
-207
lines changed

Readme.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,39 @@
1414
npm install deep-diff
1515
```
1616

17-
## Testing
18-
Tests are written using [vows](http://vowsjs.org/) & [should.js](https://github.com/visionmedia/should.js/) (you may need to install them). If you've installed in a development environment you can use npm to run the tests.
17+
## Tests
1918

19+
Tests use [mocha](http://visionmedia.github.io/mocha/) and [expect.js](https://github.com/LearnBoost/expect.js/), so if you clone the [github repository](https://github.com/flitbit/json-ptr) you'll need to run:
20+
21+
```bash
22+
npm install
2023
```
21-
npm test deep-diff
24+
25+
... followed by ...
26+
27+
```bash
28+
npm test
29+
```
30+
31+
... or ...
32+
33+
```bash
34+
mocha -R spec
2235
```
2336

24-
If you intend to run the examples you'll also need [extend](https://github.com/justmoon/node-extend) and [lodash](https://github.com/bestiejs/lodash).
37+
### Importing
2538

26-
## Warning!
39+
**nodejs**
40+
```javascript
41+
var deep = require('deep-diff')
42+
```
2743

28-
I've only had time to verify its behavior in node.js. If you're working in a browser you're on your own for now.
44+
**browser**
45+
```html
46+
<script src="deep-diff-0.1.2.min.js"></script>
47+
```
48+
49+
> In a browser, `deep-diff` defines a global variable `DeepDiff`. If there is a conflict in the global namesapce you can restore the conflicting definition and assign `deep-diff` to another variable like this: `var deep = DeepDiff.noConflict();`.
2950
3051
## Simple Examples
3152

@@ -54,7 +75,7 @@ var rhs = {
5475
}
5576
};
5677

57-
var differences = diff(lhs, rhs);
78+
var differences = diff(lhs, rhs);
5879
```
5980
The code snippet above would result in the following structure describing the differences:
6081
``` javascript
@@ -89,7 +110,7 @@ Differences are reported as one or more change records. Change records have the
89110
* `lhs` - the value on the left-hand-side of the comparison (undefined if kind === 'N')
90111
* `rhs` - the value on the right-hand-side of the comparison (undefined if kind === 'D')
91112
* `index` - when kind === 'A', indicates the array index where the change occurred
92-
* `item` - when kind === 'A', contains a nested change record indicating the change that occurred at the array index
113+
* `item` - when kind === 'A', contains a nested change record indicating the change that occurred at the array index
93114

94115
Change records are generated for all structural differences between `origin` and `comparand`. The methods only consider an object's own properties and array elements; those inherited from an object's prototype chain are not considered.
95116

@@ -126,12 +147,13 @@ var rhs = {
126147
observableDiff(lhs, rhs, function (d) {
127148
// Apply all changes except those to the 'name' property...
128149
if (d.path.length !== 1 || d.path.join('.') !== 'name') {
129-
applyChange(lhs, rhs, d);
150+
applyChange(lhs, rhs, d);
130151
}
131-
});
152+
});
132153
```
133154

134155
## API Documentation
156+
135157
A standard import of `var diff = require('deep-diff')` is assumed in all of the code examples. The import results in an object having the following public properties:
136158

137159
* `diff` - a function that calculates the differences between two objects.

examples/performance.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var util = require('util')
2+
, diff = require('..')
3+
, data = require('./practice-data')
4+
;
5+
6+
var cycle = -1
7+
, i
8+
, len = data.length
9+
, prior = {}
10+
, comparand
11+
, records
12+
, roll = []
13+
, ch
14+
, stat
15+
, stats = []
16+
, mark, elapsed, avg = { diff: { ttl: 0 }, apply: { ttl: 0 } }, ttl = 0
17+
;
18+
19+
mark = process.hrtime();
20+
while(++cycle < 10) {
21+
i = -1;
22+
while(++i < len) {
23+
stats.push(stat = { mark: process.hrtime() });
24+
25+
comparand = roll[i] || data[i];
26+
27+
stat.diff = { mark: process.hrtime() };
28+
records = diff(prior, comparand);
29+
stat.diff.intv = process.hrtime(stat.diff.mark);
30+
31+
if (records) {
32+
stat.apply = { count: diff.length, mark: process.hrtime() };
33+
records.forEach(function(ch) {
34+
diff.applyChange(prior, comparand, ch);
35+
});
36+
stat.apply.intv = process.hrtime(stat.apply.mark);
37+
38+
prior = comparand;
39+
}
40+
stat.intv = process.hrtime(stat.mark);
41+
}
42+
}
43+
44+
function ms(intv) {
45+
return (intv[0]*1e9 + intv[1]/1e6);
46+
}
47+
elapsed = ms(process.hrtime(mark));
48+
49+
stats.forEach(function(stat) {
50+
stat.elapsed = ms(stat.intv);
51+
stat.diff.elapsed = ms(stat.diff.intv);
52+
avg.diff.ttl += stat.diff.elapsed;
53+
if (stat.apply) {
54+
stat.apply.elapsed = ms(stat.apply.intv);
55+
ttl += stat.apply.count;
56+
avg.apply.ttl += stat.apply.elapsed;
57+
}
58+
});
59+
60+
avg.diff.avg = avg.diff.ttl / ttl;
61+
avg.apply.avg = avg.apply.ttl / ttl;
62+
63+
console.log('Captured '.concat(stats.length, ' samples with ', ttl, ' combined differences in ', elapsed, 'ms'));
64+
console.log('\tavg diff: '.concat(avg.diff.avg, 'ms or ', (1 / avg.diff.avg), ' per ms'));
65+
console.log('\tavg apply: '.concat(avg.apply.avg, 'ms or ', (1 / avg.apply.avg), ' per ms'));

0 commit comments

Comments
 (0)