0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/buffers/buffer-write-string.js
Ruben Bridgewater 8e3d7623a5
benchmark: (buffers) use destructuring
PR-URL: https://github.com/nodejs/node/pull/18250
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-01-23 01:29:31 +01:00

66 lines
1.4 KiB
JavaScript

'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({ len, n, encoding, 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);
}
}
}