Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!