-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
50 lines (37 loc) · 1.34 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
const assert = require('assert');
const { Suite } = require('benchmark');
const naiive = x => JSON.parse(JSON.stringify(x));
const contenders = {
'JSON.stringify': naiive,
'fast-clone': require('fast-clone'),
'lodash': require('lodash/clonedeep'),
'clone-deep': require('clone-deep'),
'deep-copy': require('deep-copy'),
'klona': require('../dist/klona')
};
console.log('Validation: ');
Object.keys(contenders).forEach(name => {
const INPUT = require('./input');
try {
const output = contenders[name](INPUT);
assert.deepStrictEqual(output, INPUT, 'initial copy');
output[0].age++;
assert.notEqual(INPUT[0].age, 34, 'increment');
output[1].details.address.coords.longitude = 100;
assert.notEqual(INPUT[1].details.address.coords.longitude, 100, 'nested assignment');
output[0].friends[1].friends_common.friends_of_friends.push('BOB');
assert.equal(INPUT[0].friends[1].friends_common.friends_of_friends.includes('BOB'), false, 'nested push');
console.log(' ✔', name);
} catch (err) {
console.log(' ✘', name, `(FAILED @ "${err.message}")`);
}
});
console.log('\nBenchmark:');
const INPUT = require('./input');
const bench = new Suite().on('cycle', e => {
console.log(' ' + e.target);
});
Object.keys(contenders).forEach(name => {
bench.add(name + ' '.repeat(22 - name.length), () => contenders[name](INPUT))
});
bench.run();