-
Notifications
You must be signed in to change notification settings - Fork 29.6k
/
buffer-write-string.js
70 lines (65 loc) · 1.46 KB
/
buffer-write-string.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
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
encoding: [
'', 'utf8', 'ascii', 'hex', 'UCS-2', 'utf16le', 'latin1', 'binary'
],
args: [ '', 'offset', 'offset+length' ],
len: [10, 2048],
n: [1e7]
});
function main(conf) {
const len = +conf.len;
const n = +conf.n;
const encoding = conf.encoding;
const args = conf.args;
const string = 'a'.repeat(len);
const buf = Buffer.allocUnsafe(len);
var i;
switch (args) {
case 'offset':
if (encoding) {
bench.start();
for (i = 0; i < n; ++i) {
buf.write(string, 0, encoding);
}
bench.end(n);
} else {
bench.start();
for (i = 0; i < n; ++i) {
buf.write(string, 0);
}
bench.end(n);
}
break;
case 'offset+length':
if (encoding) {
bench.start();
for (i = 0; i < n; ++i) {
buf.write(string, 0, buf.length, encoding);
}
bench.end(n);
} else {
bench.start();
for (i = 0; i < n; ++i) {
buf.write(string, 0, buf.length);
}
bench.end(n);
}
break;
default:
if (encoding) {
bench.start();
for (i = 0; i < n; ++i) {
buf.write(string, encoding);
}
bench.end(n);
} else {
bench.start();
for (i = 0; i < n; ++i) {
buf.write(string);
}
bench.end(n);
}
}
}