-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbenchmark.js
More file actions
63 lines (46 loc) · 1.79 KB
/
benchmark.js
File metadata and controls
63 lines (46 loc) · 1.79 KB
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
var bignumJSON = require('./lib');
var NUM_CALLS = 10000;
var NUM_TRIALS = 10;
var jsonStr = '{ \
"bigint": 92233720368547758074237, \
"bigdecimal": -9223372036854775807.4237482374983253298159, \
"string": "hello world", \
"boolean": true, \
"array": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], \
"object": { \
"int": 15, \
"float": 15.56 \
} \
}';
function runTest(func, input) {
var total = 0;
for (var i = 0; i < NUM_TRIALS; i++) {
var startTime = process.hrtime();
for (var j = 0; j < NUM_CALLS; j++) {
func(input);
}
var duration = process.hrtime(startTime);
duration = (duration[0] * 1e9 + duration[1]) / 1e3 / NUM_CALLS;
total += duration;
}
return (total / NUM_TRIALS);
}
// dont count first call
var jsonObj = JSON.parse(jsonStr);
var bignumObj = bignumJSON.parse(jsonStr);
JSON.stringify(jsonObj);
bignumJSON.stringify(bignumObj);
console.log('\n==========================================================');
console.log(' Time is averaged over ' + NUM_TRIALS + ' trials where the function');
console.log(' is called ' + NUM_CALLS + ' times.');
console.log('==========================================================\n');
var duration = runTest(JSON.parse, jsonStr);
console.log('JSON.parse(): ' + duration.toString() + ' microseconds/call');
duration = runTest(bignumJSON.parse, jsonStr);
console.log('bignumJSON.parse(): ' + duration.toString() + ' microseconds/call');
console.log();
duration = runTest(JSON.stringify, jsonObj);
console.log('JSON.stringify(): ' + duration.toString() + ' microseconds/call');
duration = runTest(bignumJSON.stringify, bignumObj);
console.log('bignumJSON.stringify(): ' + duration.toString() + ' microseconds/call');
console.log();