Skip to content

Commit

Permalink
benchmark: add benchmarks for Buffer.from()
Browse files Browse the repository at this point in the history
Adds benchmarks for `Buffer.from()` and its various
argument combinations.

Ref: nodejs#8733
PR-URL: nodejs#8738
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
addaleax committed Sep 26, 2016
1 parent 558a884 commit 289d862
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions benchmark/buffers/buffer-from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
source: [
'array',
'arraybuffer',
'arraybuffer-middle',
'buffer',
'uint8array',
'string',
'string-base64'
],
len: [10, 2048],
n: [1024]
});

function main(conf) {
const len = +conf.len;
const n = +conf.n;

const array = new Array(len).fill(42);
const arrayBuf = new ArrayBuffer(len);
const str = 'a'.repeat(len);
const buffer = Buffer.allocUnsafe(len);
const uint8array = new Uint8Array(len);

var i;

switch (conf.source) {
case 'array':
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(array);
}
bench.end(n);
break;
case 'arraybuffer':
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(arrayBuf);
}
bench.end(n);
break;
case 'arraybuffer-middle':
const offset = ~~(len / 4);
const length = ~~(len / 2);
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(arrayBuf, offset, length);
}
bench.end(n);
break;
case 'buffer':
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(buffer);
}
bench.end(n);
break;
case 'uint8array':
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(uint8array);
}
bench.end(n);
break;
case 'string':
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(str);
}
bench.end(n);
break;
case 'string-base64':
bench.start();
for (i = 0; i < n * 1024; i++) {
Buffer.from(str, 'base64');
}
bench.end(n);
break;
default:
assert.fail(null, null, 'Should not get here');
}
}

0 comments on commit 289d862

Please sign in to comment.