Skip to content

Commit

Permalink
feat(perf): cache iconv decoder (#2391)
Browse files Browse the repository at this point in the history
* Cache with Map.

* Cache with lru-cache.

* Require default explicitly.
  • Loading branch information
sandinmyjoints authored Jan 23, 2024
1 parent af47148 commit b95b3db
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/parsers/string.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
'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();

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);
}
Expand Down

0 comments on commit b95b3db

Please sign in to comment.