Use Map for caches instead of objects

This commit is contained in:
Cohee 2024-04-23 16:15:54 +03:00
parent d1f292f462
commit 75372ad0cc
2 changed files with 19 additions and 9 deletions

View File

@ -287,8 +287,10 @@ async function synchronizeChat(batchSize = 5) {
} }
} }
// Cache object for storing hash values /**
const hashCache = {}; * @type {Map<string, number>} Cache object for storing hash values
*/
const hashCache = new Map();
/** /**
* Gets the hash value for a given string * Gets the hash value for a given string
@ -297,15 +299,15 @@ const hashCache = {};
*/ */
function getStringHash(str) { function getStringHash(str) {
// Check if the hash is already in the cache // Check if the hash is already in the cache
if (Object.hasOwn(hashCache, str)) { if (hashCache.has(str)) {
return hashCache[str]; return hashCache.get(str);
} }
// Calculate the hash value // Calculate the hash value
const hash = calculateHash(str); const hash = calculateHash(str);
// Store the hash in the cache // Store the hash in the cache
hashCache[str] = hash; hashCache.set(str, hash);
return hash; return hash;
} }

View File

@ -5,7 +5,10 @@ const TASK = 'text-classification';
const router = express.Router(); const router = express.Router();
const cacheObject = {}; /**
* @type {Map<string, object>} Cache for classification results
*/
const cacheObject = new Map();
router.post('/labels', jsonParser, async (req, res) => { router.post('/labels', jsonParser, async (req, res) => {
try { try {
@ -23,15 +26,20 @@ router.post('/', jsonParser, async (req, res) => {
try { try {
const { text } = req.body; const { text } = req.body;
/**
* Get classification result for a given text
* @param {string} text Text to classify
* @returns {Promise<object>} Classification result
*/
async function getResult(text) { async function getResult(text) {
if (Object.hasOwn(cacheObject, text)) { if (cacheObject.has(text)) {
return cacheObject[text]; return cacheObject.get(text);
} else { } else {
const module = await import('../transformers.mjs'); const module = await import('../transformers.mjs');
const pipe = await module.default.getPipeline(TASK); const pipe = await module.default.getPipeline(TASK);
const result = await pipe(text, { topk: 5 }); const result = await pipe(text, { topk: 5 });
result.sort((a, b) => b.score - a.score); result.sort((a, b) => b.score - a.score);
cacheObject[text] = result; cacheObject.set(text, result);
return result; return result;
} }
} }