-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add benchmarks for
Buffer.from()
Adds benchmarks for `Buffer.from()` and its various argument combinations. Ref: #8733 PR-URL: #8738 Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
- Loading branch information
1 parent
b9c2270
commit 1a6e898
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |