| 17 | const ASCII_ALPHA = 'Blueberry jam'; |
| 18 | |
| 19 | function main({ encoding, len, unicode, chunks, n, ignoreBOM, type, fatal }) { |
| 20 | const decoder = new TextDecoder(encoding, { ignoreBOM, fatal }); |
| 21 | let buf; |
| 22 | |
| 23 | const fill = Buffer.from(unicode ? UNICODE_ALPHA : ASCII_ALPHA, encoding); |
| 24 | |
| 25 | switch (type) { |
| 26 | case 'SharedArrayBuffer': { |
| 27 | buf = new SharedArrayBuffer(len); |
| 28 | Buffer.from(buf).fill(fill); |
| 29 | break; |
| 30 | } |
| 31 | case 'ArrayBuffer': { |
| 32 | buf = new ArrayBuffer(len); |
| 33 | Buffer.from(buf).fill(fill); |
| 34 | break; |
| 35 | } |
| 36 | case 'Buffer': { |
| 37 | buf = Buffer.alloc(len, fill); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const chunk = Math.ceil(len / chunks); |
| 43 | const max = len - chunk; |
| 44 | bench.start(); |
| 45 | for (let i = 0; i < n; i++) { |
| 46 | let pos = 0; |
| 47 | while (pos < max) { |
| 48 | decoder.decode(buf.slice(pos, pos + chunk), { stream: true }); |
| 49 | pos += chunk; |
| 50 | } |
| 51 | |
| 52 | decoder.decode(buf.slice(pos)); |
| 53 | } |
| 54 | bench.end(n); |
| 55 | } |