-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathundef.js
60 lines (51 loc) · 1.37 KB
/
undef.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
/**
* Testing `SJS` performance against native `JSON.stringify` and the fastest
* stringifier in town `fast-json-stringify`
*
* Testing for undefined properties.
*/
const Benchmark = require('benchmark');
const fjs = require('fast-json-stringify');
const { sjs, attr } = require('../dist/sjs');
const suite = new Benchmark.Suite;
const obj = { hello: 'world', gino: 'The answer is 42', jimmy: undefined };
// Fast-json-stringify schema
const fastStringify = fjs({
type: 'object',
properties: {
hello: {
type: 'string',
},
gino: {
type: 'string',
},
jimmy: {
type: 'string',
},
},
});
// Slow-json-stringify schema
const slowStringify = sjs({
hello: attr('string'),
jimmy: attr('string'),
});
const res = [];
const percentageDiff = (arr) => {
const use = arr.sort((a, b) => b - a);
return (use[0] - use[1]) / use[1] * 100;
};
console.log('```bash');
suite
.add('native', () => JSON.stringify(obj))
.add('fast-json-stringify', () => fastStringify(obj))
.add('slow-json-stringify', () => slowStringify(obj))
.on('cycle', (event) => {
res.push(Math.floor(event.target.hz));
console.log(String(event.target))
})
.on('complete', function () {
const fastest = this.filter('fastest').map('name');
console.log(`\n# ${fastest} is +${percentageDiff(res).toFixed(2)}% faster`);
console.log('\n```\n');
})
.run();