Skip to content

Commit b95b3db

Browse files
feat(perf): cache iconv decoder (#2391)
* Cache with Map. * Cache with lru-cache. * Require default explicitly.
1 parent af47148 commit b95b3db

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

lib/parsers/string.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
'use strict';
22

33
const Iconv = require('iconv-lite');
4+
const LRU = require('lru-cache').default;
45

5-
exports.decode = function(buffer, encoding, start, end, options) {
6+
const decoderCache = new LRU({
7+
max: 500,
8+
});
9+
10+
exports.decode = function (buffer, encoding, start, end, options) {
611
if (Buffer.isEncoding(encoding)) {
712
return buffer.toString(encoding, start, end);
813
}
914

10-
const decoder = Iconv.getDecoder(encoding, options || {});
15+
// Optimize for common case: encoding="short_string", options=undefined.
16+
let decoder;
17+
if (!options) {
18+
decoder = decoderCache.get(encoding);
19+
if (!decoder) {
20+
decoder = Iconv.getDecoder(encoding);
21+
decoderCache.set(encoding, decoder);
22+
}
23+
} else {
24+
const decoderArgs = { encoding, options };
25+
const decoderKey = JSON.stringify(decoderArgs);
26+
decoder = decoderCache.get(decoderKey);
27+
if (!decoder) {
28+
decoder = Iconv.getDecoder(decoderArgs.encoding, decoderArgs.options);
29+
decoderCache.set(decoderKey, decoder);
30+
}
31+
}
1132

1233
const res = decoder.write(buffer.slice(start, end));
1334
const trail = decoder.end();
1435

1536
return trail ? res + trail : res;
1637
};
1738

18-
exports.encode = function(string, encoding, options) {
39+
exports.encode = function (string, encoding, options) {
1940
if (Buffer.isEncoding(encoding)) {
2041
return Buffer.from(string, encoding);
2142
}

0 commit comments

Comments
 (0)