diff --git a/lib/parsers/string.js b/lib/parsers/string.js index 5523fb2c6a..47c9bfa8a0 100644 --- a/lib/parsers/string.js +++ b/lib/parsers/string.js @@ -1,13 +1,34 @@ 'use strict'; const Iconv = require('iconv-lite'); +const LRU = require('lru-cache').default; -exports.decode = function(buffer, encoding, start, end, options) { +const decoderCache = new LRU({ + max: 500, +}); + +exports.decode = function (buffer, encoding, start, end, options) { if (Buffer.isEncoding(encoding)) { return buffer.toString(encoding, start, end); } - const decoder = Iconv.getDecoder(encoding, options || {}); + // Optimize for common case: encoding="short_string", options=undefined. + let decoder; + if (!options) { + decoder = decoderCache.get(encoding); + if (!decoder) { + decoder = Iconv.getDecoder(encoding); + decoderCache.set(encoding, decoder); + } + } else { + const decoderArgs = { encoding, options }; + const decoderKey = JSON.stringify(decoderArgs); + decoder = decoderCache.get(decoderKey); + if (!decoder) { + decoder = Iconv.getDecoder(decoderArgs.encoding, decoderArgs.options); + decoderCache.set(decoderKey, decoder); + } + } const res = decoder.write(buffer.slice(start, end)); const trail = decoder.end(); @@ -15,7 +36,7 @@ exports.decode = function(buffer, encoding, start, end, options) { return trail ? res + trail : res; }; -exports.encode = function(string, encoding, options) { +exports.encode = function (string, encoding, options) { if (Buffer.isEncoding(encoding)) { return Buffer.from(string, encoding); }