0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/buffers/buffer-creation.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
const SlowBuffer = require('buffer').SlowBuffer;
2013-02-11 22:41:16 +01:00
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],
2013-02-11 22:41:16 +01:00
n: [1024]
});
function main({ len, n, type }) {
switch (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');
2013-02-11 22:41:16 +01:00
}
}