| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c086bdc commit 5321300
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,65 @@ | |||
| 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,10 +132,16 @@ exports.cachedResult = function cachedResult(fn) { | |||
| 132 | 132 | }; | |
| 133 | 133 | ||
| 134 | 134 | exports.kIsEncodingSymbol = Symbol('node.isEncoding'); | |
| 135 | + | ||
| 136 | + // The loop should only run at most twice, retrying with lowercased enc | ||
| 137 | + // if there is no match in the first pass. | ||
| 138 | + // We use a loop instead of branching to retry with a helper | ||
| 139 | + // function in order to avoid the performance hit. | ||
| 140 | + // Return undefined if there is no match. | ||
| 135 | 141 | exports.normalizeEncoding = function normalizeEncoding(enc) { | |
| 136 | 142 | if (!enc) return 'utf8'; | |
| 137 | - var low; | ||
| 138 | - for (;;) { | ||
| 143 | + var retried; | ||
| 144 | + while (true) { | ||
| 139 | 145 | switch (enc) { | |
| 140 | 146 | case 'utf8': | |
| 141 | 147 | case 'utf-8': | |
@@ -153,9 +159,9 @@ exports.normalizeEncoding = function normalizeEncoding(enc) { | |||
| 153 | 159 | case 'hex': | |
| 154 | 160 | return enc; | |
| 155 | 161 | default: | |
| 156 | - if (low) return; // undefined | ||
| 162 | + if (retried) return; // undefined | ||
| 157 | 163 | enc = ('' + enc).toLowerCase(); | |
| 158 | - low = true; | ||
| 164 | + retried = true; | ||
| 159 | 165 | } | |
| 160 | 166 | } | |
| 161 | 167 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments