mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
8e3d7623a5
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>
67 lines
1.4 KiB
JavaScript
67 lines
1.4 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({ 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');
|
|
}
|
|
}
|