Skip to content

Commit 93c1fac

Browse files
Cache with Map.
1 parent e6a9306 commit 93c1fac

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/parsers/string.js

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

33
const Iconv = require('iconv-lite');
4+
const decoderCache = new Map();
45

5-
exports.decode = function(buffer, encoding, start, end, options) {
6+
exports.decode = function (buffer, encoding, start, end, options) {
67
if (Buffer.isEncoding(encoding)) {
78
return buffer.toString(encoding, start, end);
89
}
910

10-
const decoder = Iconv.getDecoder(encoding, options || {});
11+
const decoderArgs = { encoding, options: options || {} };
12+
const decoder =
13+
decoderCache.get(decoderArgs) ||
14+
decoderCache
15+
.set(
16+
decoderArgs,
17+
Iconv.getDecoder(decoderArgs.encoding, decoderArgs.options),
18+
)
19+
.get(decoderArgs);
1120

1221
const res = decoder.write(buffer.slice(start, end));
1322
const trail = decoder.end();
1423

1524
return trail ? res + trail : res;
1625
};
1726

18-
exports.encode = function(string, encoding, options) {
27+
exports.encode = function (string, encoding, options) {
1928
if (Buffer.isEncoding(encoding)) {
2029
return Buffer.from(string, encoding);
2130
}

0 commit comments

Comments
 (0)