From 341770fedf77ff5b8e0c646070029152b58fc746 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 14 Feb 2018 23:48:35 +0100 Subject: lib: improve normalize encoding performance This focuses on the common case by making sure they are prioritized. It also changes some typeof checks to test for undefined since that is faster and it adds a benchmark. PR-URL: https://github.com/nodejs/node/pull/18790 Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina --- lib/string_decoder.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib/string_decoder.js') diff --git a/lib/string_decoder.js b/lib/string_decoder.js index 04d31b2607..18097be0e6 100644 --- a/lib/string_decoder.js +++ b/lib/string_decoder.js @@ -43,10 +43,12 @@ const kNativeDecoder = Symbol('kNativeDecoder'); // modules monkey-patch it to support additional encodings function normalizeEncoding(enc) { const nenc = internalUtil.normalizeEncoding(enc); - if (typeof nenc !== 'string' && - (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc))) - throw new errors.TypeError('ERR_UNKNOWN_ENCODING', enc); - return nenc || enc; + if (nenc === undefined) { + if (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc)) + throw new errors.TypeError('ERR_UNKNOWN_ENCODING', enc); + return enc; + } + return nenc; } const encodingsMap = {}; -- cgit v1.2.3