Skip to content

Commit 34bf31e

Browse files
committed
util: improve readability of normalizeEncoding
* Improve readability of util.normalizeEncoding and add some comments * Add a benchmark for util.normalizeEncoding PR-URL: #10439 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Andreas Madsen <[email protected]>
1 parent d4b749b commit 34bf31e

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

benchmark/util/normalize-encoding.js

+65
Original file line numberDiff line numberDiff 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+
}

lib/internal/util.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,16 @@ exports.assertCrypto = function(exports) {
103103
};
104104

105105
exports.kIsEncodingSymbol = Symbol('node.isEncoding');
106+
107+
// The loop should only run at most twice, retrying with lowercased enc
108+
// if there is no match in the first pass.
109+
// We use a loop instead of branching to retry with a helper
110+
// function in order to avoid the performance hit.
111+
// Return undefined if there is no match.
106112
exports.normalizeEncoding = function normalizeEncoding(enc) {
107113
if (!enc) return 'utf8';
108-
var low;
109-
for (;;) {
114+
var retried;
115+
while (true) {
110116
switch (enc) {
111117
case 'utf8':
112118
case 'utf-8':
@@ -124,9 +130,9 @@ exports.normalizeEncoding = function normalizeEncoding(enc) {
124130
case 'hex':
125131
return enc;
126132
default:
127-
if (low) return; // undefined
133+
if (retried) return; // undefined
128134
enc = ('' + enc).toLowerCase();
129-
low = true;
135+
retried = true;
130136
}
131137
}
132138
};

0 commit comments

Comments
 (0)