|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | +const assert = require('assert'); |
| 5 | + |
| 6 | +const groupedInputs = { |
| 7 | + group_common: ['undefined', 'utf8', 'utf-8', 'base64', 'binary', 'latin1'], |
| 8 | + group_upper: ['UTF-8', 'UTF8', 'UCS2', 'UTF-16LE', 'UTF16LE', 'BASE64'], |
| 9 | + group_uncommon: [ 'foo', '1', 'false', 'undefined', '[]'], |
| 10 | + group_misc: ['', 'utf16le', 'usc2', 'hex', 'HEX', 'BINARY'] |
| 11 | +}; |
| 12 | + |
| 13 | +const inputs = [ |
| 14 | + '', 'utf8', 'utf-8', 'UTF-8', |
| 15 | + 'UTF8', 'Utf8', 'uTf-8', 'utF-8', 'ucs2', |
| 16 | + 'UCS2', 'utf16le', 'utf-16le', 'UTF-16LE', 'UTF16LE', |
| 17 | + 'binary', 'BINARY', 'latin1', 'base64', 'BASE64', |
| 18 | + 'hex', 'HEX', 'foo', '1', 'false', 'undefined', '[]']; |
| 19 | + |
| 20 | +const bench = common.createBenchmark(main, { |
| 21 | + input: inputs.concat(Object.keys(groupedInputs)), |
| 22 | + n: [1e5] |
| 23 | +}, { |
| 24 | + flags: '--expose-internals' |
| 25 | +}); |
| 26 | + |
| 27 | +function getInput(input) { |
| 28 | + switch (input) { |
| 29 | + case 'group_common': |
| 30 | + return groupedInputs.group_common; |
| 31 | + case 'group_upper': |
| 32 | + return groupedInputs.group_upper; |
| 33 | + case 'group_uncommon': |
| 34 | + return groupedInputs.group_uncommon; |
| 35 | + case 'group_misc': |
| 36 | + return groupedInputs.group_misc; |
| 37 | + case '1': |
| 38 | + return [1]; |
| 39 | + case 'false': |
| 40 | + return [false]; |
| 41 | + case 'undefined': |
| 42 | + return [undefined]; |
| 43 | + case '[]': |
| 44 | + return [[]]; |
| 45 | + default: |
| 46 | + return [input]; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function main(conf) { |
| 51 | + const normalizeEncoding = require('internal/util').normalizeEncoding; |
| 52 | + |
| 53 | + const n = conf.n | 0; |
| 54 | + const inputs = getInput(conf.input); |
| 55 | + var noDead = ''; |
| 56 | + |
| 57 | + bench.start(); |
| 58 | + for (var i = 0; i < n; i += 1) { |
| 59 | + for (var j = 0; j < inputs.length; ++j) { |
| 60 | + noDead = normalizeEncoding(inputs[j]); |
| 61 | + } |
| 62 | + } |
| 63 | + bench.end(n); |
| 64 | + assert.ok(noDead === undefined || noDead.length > 0); |
| 65 | +} |
0 commit comments