|
1 | 1 | 'use strict';
|
2 |
| -var common = require('../common.js'); |
3 |
| -var StringDecoder = require('string_decoder').StringDecoder; |
| 2 | +const common = require('../common.js'); |
| 3 | +const StringDecoder = require('string_decoder').StringDecoder; |
4 | 4 |
|
5 |
| -var bench = common.createBenchmark(main, { |
6 |
| - encoding: ['ascii', 'utf8', 'base64-utf8', 'base64-ascii'], |
7 |
| - inlen: [32, 128, 1024], |
| 5 | +const bench = common.createBenchmark(main, { |
| 6 | + encoding: ['ascii', 'utf8', 'base64-utf8', 'base64-ascii', 'utf16le'], |
| 7 | + inlen: [32, 128, 1024, 4096], |
8 | 8 | chunk: [16, 64, 256, 1024],
|
9 |
| - n: [25e4] |
| 9 | + n: [25e5] |
10 | 10 | });
|
11 | 11 |
|
12 |
| -var UTF_ALPHA = 'Blåbærsyltetøy'; |
13 |
| -var ASC_ALPHA = 'Blueberry jam'; |
| 12 | +const UTF8_ALPHA = 'Blåbærsyltetøy'; |
| 13 | +const ASC_ALPHA = 'Blueberry jam'; |
| 14 | +const UTF16_BUF = Buffer.from('Blåbærsyltetøy', 'utf16le'); |
14 | 15 |
|
15 | 16 | function main(conf) {
|
16 |
| - var encoding = conf.encoding; |
17 |
| - var inLen = conf.inlen | 0; |
18 |
| - var chunkLen = conf.chunk | 0; |
19 |
| - var n = conf.n | 0; |
| 17 | + const encoding = conf.encoding; |
| 18 | + const inLen = conf.inlen | 0; |
| 19 | + const chunkLen = conf.chunk | 0; |
| 20 | + const n = conf.n | 0; |
20 | 21 |
|
21 | 22 | var alpha;
|
22 |
| - var chunks = []; |
| 23 | + var buf; |
| 24 | + const chunks = []; |
23 | 25 | var str = '';
|
24 |
| - var isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8'); |
| 26 | + const isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8'); |
25 | 27 | var i;
|
26 | 28 |
|
27 | 29 | if (encoding === 'ascii' || encoding === 'base64-ascii')
|
28 | 30 | alpha = ASC_ALPHA;
|
29 | 31 | else if (encoding === 'utf8' || encoding === 'base64-utf8')
|
30 |
| - alpha = UTF_ALPHA; |
31 |
| - else |
| 32 | + alpha = UTF8_ALPHA; |
| 33 | + else if (encoding === 'utf16le') { |
| 34 | + buf = UTF16_BUF; |
| 35 | + str = Buffer.alloc(0); |
| 36 | + } else |
32 | 37 | throw new Error('Bad encoding');
|
33 | 38 |
|
34 |
| - var sd = new StringDecoder(isBase64 ? 'base64' : encoding); |
| 39 | + const sd = new StringDecoder(isBase64 ? 'base64' : encoding); |
35 | 40 |
|
36 | 41 | for (i = 0; i < inLen; ++i) {
|
37 | 42 | if (i > 0 && (i % chunkLen) === 0 && !isBase64) {
|
38 |
| - chunks.push(Buffer.from(str, encoding)); |
39 |
| - str = ''; |
| 43 | + if (alpha) { |
| 44 | + chunks.push(Buffer.from(str, encoding)); |
| 45 | + str = ''; |
| 46 | + } else { |
| 47 | + chunks.push(str); |
| 48 | + str = Buffer.alloc(0); |
| 49 | + } |
| 50 | + } |
| 51 | + if (alpha) |
| 52 | + str += alpha[i % alpha.length]; |
| 53 | + else { |
| 54 | + var start = i; |
| 55 | + var end = i + 2; |
| 56 | + if (i % 2 !== 0) { |
| 57 | + ++start; |
| 58 | + ++end; |
| 59 | + } |
| 60 | + str = Buffer.concat([ |
| 61 | + str, |
| 62 | + buf.slice(start % buf.length, end % buf.length) |
| 63 | + ]); |
40 | 64 | }
|
41 |
| - str += alpha[i % alpha.length]; |
42 | 65 | }
|
43 |
| - if (str.length > 0 && !isBase64) |
| 66 | + |
| 67 | + if (!alpha) { |
| 68 | + if (str.length > 0) |
| 69 | + chunks.push(str); |
| 70 | + } else if (str.length > 0 && !isBase64) |
44 | 71 | chunks.push(Buffer.from(str, encoding));
|
| 72 | + |
45 | 73 | if (isBase64) {
|
46 | 74 | str = Buffer.from(str, 'utf8').toString('base64');
|
47 | 75 | while (str.length > 0) {
|
48 |
| - var len = Math.min(chunkLen, str.length); |
| 76 | + const len = Math.min(chunkLen, str.length); |
49 | 77 | chunks.push(Buffer.from(str.substring(0, len), 'utf8'));
|
50 | 78 | str = str.substring(len);
|
51 | 79 | }
|
|
0 commit comments