-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
108 lines (88 loc) · 2.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { join } = require('path');
const { Suite } = require('benchmark');
const klona = require('klona');
console.log('Load times:');
console.time('assert');
const { deepStrictEqual } = require('assert');
console.timeEnd('assert');
console.time('util');
const { isDeepStrictEqual } = require('util');
console.timeEnd('util');
console.time('deep-equal');
const deepEqual = require('deep-equal');
console.timeEnd('deep-equal');
console.time('fast-deep-equal');
const fastdeep = require('fast-deep-equal');
console.timeEnd('fast-deep-equal');
console.time('lodash/isEqual');
const lodash = require('lodash/isEqual');
console.timeEnd('lodash/isEqual');
console.time('nano-equal');
const nanoequal = require('nano-equal');
console.timeEnd('nano-equal');
console.time('dequal');
const { dequal } = require('dequal');
console.timeEnd('dequal');
console.time('dequal/lite');
const lite = require('dequal/lite');
console.timeEnd('dequal/lite');
function naiive(a, b) {
try {
deepStrictEqual(a, b);
return true;
} catch (err) {
return false;
}
}
// @ts-ignore
const assert = (foo, bar, msg='') => deepStrictEqual(foo, bar, msg);
function runner(name, contenders) {
const file = join(__dirname, 'fixtures', name + '.js');
const fixture = require(file);
console.log('\n(%s) Validation: ', name);
Object.keys(contenders).forEach(name => {
const func = contenders[name];
const { foo, bar } = klona(fixture);
try {
assert(func(1, 1), true, 'equal numbers');
assert(func(1, 2), false, 'not equal numbers');
assert(func(1, [1]), false, 'number vs array');
assert(func(0, null), false, 'number vs null');
assert(func(0, undefined), false, 'number vs undefined');
assert(func(foo, bar), true, 'kitchen sink');
console.log(' ✔', name);
} catch (err) {
console.log(' ✘', name, `(FAILED @ "${err.message}")`);
}
});
console.log('\n(%s) Benchmark: ', name);
const bench = new Suite().on('cycle', e => {
console.log(' ' + e.target);
});
Object.keys(contenders).forEach(name => {
const { foo, bar } = klona(fixture);
bench.add(name + ' '.repeat(22 - name.length), () => {
// contenders[name]({ a: 1, b: 2, c: 3 }, { a: 1, b: 4, c: 3 });
contenders[name](foo, bar);
})
});
bench.run();
}
runner('basic', {
'assert.deepStrictEqual': naiive,
'util.isDeepStrictEqual': isDeepStrictEqual,
'deep-equal': deepEqual,
'fast-deep-equal': fastdeep,
'lodash.isEqual': lodash,
'nano-equal': nanoequal,
'dequal/lite': lite.dequal,
'dequal': dequal,
});
// Only keep those that pass
runner('complex', {
'assert.deepStrictEqual': naiive,
'util.isDeepStrictEqual': isDeepStrictEqual,
'deep-equal': deepEqual,
'lodash.isEqual': lodash,
'dequal': dequal,
});