-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
32 lines (26 loc) · 1.11 KB
/
bench.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
const Shortening = require('./shortening');
const MAX_INT = Shortening.MAX_INT;
const Short32 = Shortening.Std32;
const Short64 = Shortening.Std64;
const Benchmark = require('benchmark');
const benchSet = 4096;
const rng = (max) => { return Math.floor(Math.random()*max); };
const test_n32 = new Array(benchSet).fill(MAX_INT).map(rng);
const test_n64 = new Array(benchSet).fill(MAX_INT).map(rng);
const test_b32 = test_n32.map(Short32.Encode);
const test_b64 = test_n64.map(Short64.Encode);
const suite = new Benchmark.Suite;
suite.add('Short32.Encode', function() {
for (var i = 0; i < benchSet; i++) Short32.Encode(test_n32[i]);
}).add('Short32.Decode', function() {
for (var i = 0; i < benchSet; i++) Short32.Decode(test_b32[i]);
}).add('Short64.Encode', function() {
for (var i = 0; i < benchSet; i++) Short64.Encode(test_n64[i]);
}).add('Short64.Decode', function() {
for (var i = 0; i < benchSet; i++) Short64.Decode(test_b64[i]);
}).on('cycle', function(event) {
let bench = event.target;
bench.hz *= benchSet;
console.log(bench.name, (1e9/bench.hz).toFixed(2), "ns/op");
console.log(String(event.target));
}).run();