2016-02-20 01:03:16 +00:00
|
|
|
'use strict';
|
2013-02-11 21:41:16 +00:00
|
|
|
|
2016-01-25 23:00:06 +00:00
|
|
|
const common = require('../common.js');
|
|
|
|
const assert = require('assert');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
type: [
|
|
|
|
'fast-alloc',
|
|
|
|
'fast-alloc-fill',
|
|
|
|
'fast-allocUnsafe',
|
2016-03-21 19:38:08 +00:00
|
|
|
'slow-allocUnsafe',
|
2019-03-04 00:34:00 +00:00
|
|
|
],
|
|
|
|
len: [10, 1024, 4096, 8192],
|
2023-02-04 18:19:25 +00:00
|
|
|
n: [6e5],
|
2013-02-11 21:41:16 +00:00
|
|
|
});
|
|
|
|
|
2017-12-30 02:59:57 +00:00
|
|
|
function main({ len, n, type }) {
|
2018-01-23 12:17:39 +00:00
|
|
|
let fn, i;
|
2017-12-30 02:59:57 +00:00
|
|
|
switch (type) {
|
2016-01-25 23:00:06 +00:00
|
|
|
case 'fast-alloc':
|
2018-01-23 12:17:39 +00:00
|
|
|
fn = Buffer.alloc;
|
2016-01-25 23:00:06 +00:00
|
|
|
break;
|
|
|
|
case 'fast-alloc-fill':
|
|
|
|
bench.start();
|
2019-03-04 00:34:00 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2016-01-25 23:00:06 +00:00
|
|
|
Buffer.alloc(len, 0);
|
|
|
|
}
|
|
|
|
bench.end(n);
|
2018-01-23 12:17:39 +00:00
|
|
|
return;
|
2016-01-25 23:00:06 +00:00
|
|
|
case 'fast-allocUnsafe':
|
2018-01-23 12:17:39 +00:00
|
|
|
fn = Buffer.allocUnsafe;
|
2016-01-25 23:00:06 +00:00
|
|
|
break;
|
2016-03-21 19:38:08 +00:00
|
|
|
case 'slow-allocUnsafe':
|
2018-01-23 12:17:39 +00:00
|
|
|
fn = Buffer.allocUnsafeSlow;
|
2016-03-21 19:38:08 +00:00
|
|
|
break;
|
2016-01-25 23:00:06 +00:00
|
|
|
default:
|
2018-01-23 12:17:39 +00:00
|
|
|
assert.fail('Should not get here');
|
|
|
|
}
|
|
|
|
|
|
|
|
bench.start();
|
2019-03-04 00:34:00 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2018-01-23 12:17:39 +00:00
|
|
|
fn(len);
|
2013-02-11 21:41:16 +00:00
|
|
|
}
|
2018-01-23 12:17:39 +00:00
|
|
|
bench.end(n);
|
2013-02-11 21:41:16 +00:00
|
|
|
}
|