-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbenchmark.js
69 lines (56 loc) · 1.71 KB
/
benchmark.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
// Testing Node.js's speed for various functions:
// Built-in functions for assisting:
// http://nodejs.org/api/process.html#process_process_hrtime
// http://nodejs.org/api/console.html#console_console_time_label
var bignum = require('bignum');
var rounds = 20000;
console.log('Base58 encoding');
var chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
var data = new Buffer('15e1a259dbef84baa4c9', 'hex'); // '2EJVBL6PvHq9v8' encoded
console.log('data:', data);
var base = chars.length;
function base58encode_array(data) {
var num = bignum.fromBuffer(data);
var encoded = [];
while(num.gt(0)) {
encoded.push(chars.charAt(num.mod(base)));
num = num.div(base);
}
return encoded.reverse().join('');
}
console.log('encoded (array)', base58encode_array(data));
console.time('base58-array');
for (var i = 0; i < rounds; i++) {
base58encode_array(data);
}
console.timeEnd('base58-array');
function base58encode_rarray(data) {
var num = bignum.fromBuffer(data);
var encoded = [];
while(num.gt(0)) {
encoded.unshift(chars.charAt(num.mod(base)));
num = num.div(base);
}
return encoded.join('');
}
console.log('encoded (reversed-array)', base58encode_rarray(data));
console.time('base58-reversed-array');
for (var i = 0; i < rounds; i++) {
base58encode_rarray(data);
}
console.timeEnd('base58-reversed-array');
function base58encode_string(data) {
var num = bignum.fromBuffer(data);
var encoded = '';
while(num.gt(0)) {
encoded = chars.charAt(num.mod(base))+encoded;
num = num.div(base);
}
return encoded
}
console.log('encoded (string)', base58encode_string(data));
console.time('base58-string');
for (var i = 0; i < rounds; i++) {
base58encode_string(data);
}
console.timeEnd('base58-string');