-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add writable stream encoding benchmark
- Loading branch information
Showing
1 changed file
with
35 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,35 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const Writable = require('stream').Writable; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [50000], | ||
inputType: ['buffer', 'string'], | ||
inputEncoding: ['utf8', 'ucs2'], | ||
decodeAs: ['', 'utf8', 'ucs2'] | ||
}); | ||
|
||
const inputStr = `袎逜 釂鱞鸄 碄碆碃 蒰裧頖 鋑, 瞂 覮轀 蔝蓶蓨 踥踕踛 鬐鶤 鄜 忁曨曣 | ||
翀胲胵, 鬵鵛嚪 釢髟偛 碞碠粻 漀 涾烰 跬 窱縓 墥墡嬇 禒箈箑, 餤駰 瀁瀎瀊 躆轖轕 蒛, 銙 簎艜薤 | ||
樆樦潏 魡鳱 櫱瀯灂 鷜鷙鷵 禒箈箑 綧 駓駗, 鋡 嗛嗕塨 嶭嶴憝 爂犤繵 罫蓱 摮 灉礭蘠 蠬襱覾 脬舑莕 | ||
躐鑏, 襆贂 漀 刲匊呥 肒芅邥 泏狔狑, 瀗犡礝 浘涀缹 輲輹 綧`; | ||
|
||
function main(conf) { | ||
const n = +conf.n; | ||
const s = new Writable({ | ||
decodeBuffers: !!conf.decodeAs, | ||
defaultEncoding: conf.decodeAs || undefined, | ||
write(chunk, encoding, cb) { cb(); } | ||
}); | ||
|
||
const inputEnc = conf.inputType === 'buffer' ? undefined : conf.inputEncoding; | ||
const input = conf.inputType === 'buffer' ? | ||
Buffer.from(inputStr, conf.inputEncoding) : inputStr; | ||
|
||
bench.start(); | ||
for (var k = 0; k < n; ++k) { | ||
s.write(input, inputEnc); | ||
} | ||
bench.end(n); | ||
} |