DEV Community

Discussion on: Quick tip: Outsource heavy calculations if possible

Collapse
 
somedood profile image
Basti Ortiz

Whenever necessary, I also employ currying for my functions so that I wouldn't have to insert the same arguments over and over again. I'm pretty sure it's a "micro-optimization", but it does save me from the verbosity.

function someHeavyComputation(n) {
  let sum = 0;
  for (let i = 0; i < n; ++I)
    sum += i;

  return function() {
    // Do something with `sum`...
  }
}

// Cache the computation
const getBigNumber = someHeavyComputation(1000);

console.log(getBigNumber());