| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,55 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common.js'); | ||
| 4 | + | ||
| 5 | + const bench = common.createBenchmark(main, { | ||
| 6 | + encoding: ['utf-8', 'utf-16le'], | ||
| 7 | + ignoreBOM: [0, 1], | ||
| 8 | + fatal: [0, 1], | ||
| 9 | + unicode: [0, 1], | ||
| 10 | + len: [256, 1024 * 16, 1024 * 128], | ||
| 11 | + chunks: [10], | ||
| 12 | + n: [1e3], | ||
| 13 | + type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'], | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + const UNICODE_ALPHA = 'Blåbærsyltetøy'; | ||
| 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments