2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
|
|
|
const SlowBuffer = require('buffer').SlowBuffer;
|
2013-02-11 22:41:16 +01:00
|
|
|
|
2016-01-26 00:00:06 +01: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 20:38:08 +01:00
|
|
|
'slow-allocUnsafe',
|
2016-01-26 00:00:06 +01:00
|
|
|
'slow',
|
|
|
|
'buffer()'],
|
|
|
|
len: [10, 1024, 2048, 4096, 8192],
|
2013-02-11 22:41:16 +01:00
|
|
|
n: [1024]
|
|
|
|
});
|
|
|
|
|
2017-12-30 03:59:57 +01:00
|
|
|
function main({ len, n, type }) {
|
|
|
|
switch (type) {
|
2017-09-03 23:58:53 +02:00
|
|
|
case '':
|
2016-01-26 00:00:06 +01:00
|
|
|
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;
|
2016-03-21 20:38:08 +01:00
|
|
|
case 'slow-allocUnsafe':
|
|
|
|
bench.start();
|
|
|
|
for (let i = 0; i < n * 1024; i++) {
|
|
|
|
Buffer.allocUnsafeSlow(len);
|
|
|
|
}
|
|
|
|
bench.end(n);
|
|
|
|
break;
|
2016-01-26 00:00:06 +01:00
|
|
|
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');
|
2013-02-11 22:41:16 +01:00
|
|
|
}
|
|
|
|
}
|