Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
Does this function leak memory

I have this program that parses a json file and assigns the keys and values to two arrays. Then it loops through each key, reads a css file and if there is a match in the css variables it rewrites the file with the corresponding value.

const fs = require('fs')
const path = require('path')

function applyConfigurationToCss() {
  let str = '';
  let regex = new RegExp(`--${str}: .*$`, 'gm')
  let theme_keys = []
  let theme_values = []
  let css_file = fs.readFileSync('../style.css', 'utf-8')

  const key_value_pairs = JSON.parse(fs.readFileSync('../theme.json'), (key, value) => {
    theme_keys.push(key)
    theme_values.push(value)
  })

  for (let i = 0; i < theme_keys.length - 2; i++) {
    str = theme_keys[i]
    console.log(str)
    regex = new RegExp(`--${str}: .*$`, 'gm')
    fs.writeFileSync('../style.css', css_file.replace(regex, `--${str}: ${theme_values[i]};`), 'utf-8')
    css_file = fs.readFileSync('../style.css', 'utf-8')
    console.log(process.memoryUsage())
  }
}

applyConfigurationToCss()

It works like it should for now, but on each cycle of the for loop the heapUsed value grows. I am just now looking more into memory management and I was wondering if this is a sign of a memory leak. Here is the program output:

color0
{
  rss: 34164736,
  heapTotal: 4980736,
  heapUsed: 4240312,
  external: 452419,
  arrayBuffers: 19350
}
color1
{
  rss: 34615296,
  heapTotal: 5513216,
  heapUsed: 4778160,
  external: 452459,
  arrayBuffers: 19350
}
color2
{
  rss: 34615296,
  heapTotal: 5513216,
  heapUsed: 4791600,
  external: 460651,
  arrayBuffers: 27542
}
color3
{
  rss: 34615296,
  heapTotal: 5513216,
  heapUsed: 4805008,
  external: 468843,
  arrayBuffers: 35734
}
bg-img
{
  rss: 34615296,
  heapTotal: 5513216,
  heapUsed: 4815472,
  external: 468843,
  arrayBuffers: 35734
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda