-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicktest.js
89 lines (85 loc) · 2.73 KB
/
quicktest.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
// eslint-disable-next-line node/no-unpublished-require
const Benchmark = require("benchmark");
const suite = new Benchmark.Suite("SCON/JSON comparison");
const {FastSconEncoder, SconEncoder, SconDecoder} = require("./");
const se = new SconEncoder({referencedStrings: true});
const fse = new FastSconEncoder();
const sd = new SconDecoder();
const crypto = require("crypto");
const testVal = {
hello: "world",
array: ["stuff", 3, 5, 7],
number: 1337,
float: 13.37,
longString: crypto.randomBytes(100000).toString("base64")
};
const testCount = 100;
for(let i = 0; i < 500; i += 1){
/* testVal[i.toString(36)] = Math.random() * 10000 | 0;
testVal[i.toString(36)] = Math.random();
testVal[(i + 500).toString(36)] = Math.random().toString(36);
testVal[(i + 1000).toString(36)] = "test"; */
}
const encodedJson = JSON.stringify(testVal);
const encodedScon = se.encode(testVal);
console.time("JSON Encode");
for(let i = 0; i < testCount; i += 1){
// eslint-disable-next-line no-unused-vars
const something = JSON.stringify(testVal);
}
console.timeEnd("JSON Encode");
console.time("SCON Encode");
for(let i = 0; i < testCount; i += 1){
// eslint-disable-next-line no-unused-vars
const something = se.encode(testVal);
}
console.timeEnd("SCON Encode");
console.time("SCON Encode (fast)");
for(let i = 0; i < testCount; i += 1){
// eslint-disable-next-line no-unused-vars
const something = fse.encode(testVal);
}
console.timeEnd("SCON Encode (fast)");
console.time("JSON Decode");
for(let i = 0; i < testCount; i += 1){
// eslint-disable-next-line no-unused-vars
const something = JSON.parse(encodedJson);
}
console.timeEnd("JSON Decode");
console.time("SCON Decode");
for(let i = 0; i < testCount; i += 1){
// eslint-disable-next-line no-unused-vars
const something = sd.decode(encodedScon);
}
console.timeEnd("SCON Decode");
if(process.argv.includes("quick")){
return;
}
suite.add("JSON Encode", () => {
// eslint-disable-next-line no-unused-vars
const something = JSON.stringify(testVal);
});
suite.add("SCON Encode", () => {
// eslint-disable-next-line no-unused-vars
const something = se.encode(testVal);
});
suite.add("SCON Encode (fast)", () => {
// eslint-disable-next-line no-unused-vars
const something = fse.encode(testVal);
});
suite.add("JSON Decode", () => {
// eslint-disable-next-line no-unused-vars
const something = JSON.parse(encodedJson);
});
suite.add("SCON Decode", () => {
// eslint-disable-next-line no-unused-vars
const something = sd.decode(encodedScon);
});
// add listeners
suite.on("cycle", event => {
console.log(String(event.target));
}).on("complete", function(){
// eslint-disable-next-line no-invalid-this
console.log(`Fastest is ${this.filter("fastest").map("name")}`);
})
.run({async: true, minTime: 30, minSamples: 100});