Skip to content

Memory leak createCachedFormatter  #2227

@tomasPavlacky

Description

@tomasPavlacky

🐛 Bug Report

Hello, I noticed Increasing memory.

To Reproduce

import i18next from 'i18next';

i18next.init({
  lng: 'cs', 
  debug: true,
  resources: {
    cs: {
      translation: {
        "intlDateTime": "{{- val, datetime}}",
      }
    }
  }
});

setInterval(() => {
    for (let i= 0; i< 100_000; i++){
      i18next.t('intlDateTime', {val: new Date(), formatParams: { val: { 
year: 'numeric', 
month: 'long', 
day: 'numeric', 
hour: 'numeric', 
minute: 'numeric', 
second: 'numeric' ,
milisecond: 'numeric'
// ,faster: Math.random() // for faster simulation uncomment 
}}})
    }
}, 1);

// Mock with logging
function createCachedFormatter(fn) {
  var cache = {};
  return function invokeFormatter(val, lng, options) {
   console.log(`count of objects: ${Object.keys(cache).length}`, process.memoryUsage().heapUsed / 1024 / 1024);
    
    var key = lng + JSON.stringify(options);
    var formatter = cache[key];
    if (!formatter) {
      formatter = fn(lng, options);
      cache[key] = formatter;
    }
    return formatter(val);
  };
}

Your Environment

  • runtime version: i.e. node 20.12.1
  • i18next version: i.e. 22.5.1
  • os: Mac

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions