node/benchmark/buffers/buffer-creation.js
Rich Trott b62343d83c benchmark: add default configs to buffer benchmark
Add default values to use for `type` and `method` in `buffer` benchmarks
when the provided configuration value is an empty string. This is
primarily useful for testing, so the test can request a single iteration
without having to worry about providing different valid values for the
different benchmarks.

While making this change, some `var` instances in immediately
surrounding code were changed to `const`. In some cases, `var` had been
preserved so that the benchmarks would continue to run in versions of
Node.js prior to 4.0.0. However, now that `const` has been introduced
into the benchmark `common` module, the benchmarks will no longer run
with those versions of Node.js anyway.

PR-URL: https://github.com/nodejs/node/pull/15175
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com
2017-09-07 15:47:37 -07:00

69 lines
1.5 KiB
JavaScript

'use strict';
const SlowBuffer = require('buffer').SlowBuffer;
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
type: [
'fast-alloc',
'fast-alloc-fill',
'fast-allocUnsafe',
'slow-allocUnsafe',
'slow',
'buffer()'],
len: [10, 1024, 2048, 4096, 8192],
n: [1024]
});
function main(conf) {
const len = +conf.len;
const n = +conf.n;
switch (conf.type) {
case '':
case 'fast-alloc':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.alloc(len);
}
bench.end(n);
break;
case 'fast-alloc-fill':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.alloc(len, 0);
}
bench.end(n);
break;
case 'fast-allocUnsafe':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.allocUnsafe(len);
}
bench.end(n);
break;
case 'slow-allocUnsafe':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.allocUnsafeSlow(len);
}
bench.end(n);
break;
case 'slow':
bench.start();
for (let i = 0; i < n * 1024; i++) {
SlowBuffer(len);
}
bench.end(n);
break;
case 'buffer()':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer(len);
}
bench.end(n);
break;
default:
assert.fail(null, null, 'Should not get here');
}
}