-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny.js
64 lines (61 loc) · 1.72 KB
/
tiny.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
'use strict';
const util = require('util');
const benchmark = require('benchmark');
const dumpster = require('../');
const jss = require('json-stringify-safe');
const cjson = require('circular-json');
const sample = {
field1: {
a: 1,
b: 2,
inner: {
some: {
other: {
object: {
re: /re/
}
}
}
},
f: [function () {}]
},
array: [{
x: 1,
c: true,
s: new RegExp()
}, {
z: null
}, {
t: [[1, 2, 3], [10, [20, [30, [40]]]]]
}]
};
const tinyJSONBench = new benchmark.Suite();
tinyJSONBench.add('dumpster (depth: 2)', () => {
dumpster.dump(sample, { depth: 2 });
}).add('dumpster (depth: Infinity)', () => {
dumpster.dump(sample, { depth: Infinity });
}).add('dumpster (pretty)', () => {
dumpster.dump(sample, { pretty: true, depth: Infinity });
}).add('JSON.stringify', () => {
JSON.stringify(sample);
}).add('JSON.stringify (pretty)', () => {
JSON.stringify(sample, null, 4);
}).add('util.inspect (depth: 2)', () => {
util.inspect(sample, { depth: 2 });
}).add('util.inspect (depth: Infinity)', () => {
util.inspect(sample, { depth: null });
}).add('util.format (%j)', () => {
util.format('%j', sample);
}).add('json-stringify-safe', function () {
jss(sample);
}).add('json-stringify-safe (pretty)', function () {
jss(sample, null, 4);
}).add('circular-json', function () {
cjson.stringify(sample);
}).add('circular-json (pretty)', function () {
cjson.stringify(sample, null, 4);
}).on('error', e => {
console.log(e);
}).on('cycle', event => {
console.log(String(event.target));
}).run();