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

158
Vistas
Javascript Memoization Implementation

I've been debating this topic with my mates at work. I would like to know from you guys, if this is, actually, the right way of implementing the memoization.

function memoize(result) {
  let cache = {};

  return function() {
    if (cache[result]) {
      // returns cached result / no calculation
      return cache[result];
    }
    // calculating...
    cache[result] = result;
    return cache[result];
  };
}

function expensiveCalculation() {
  let counter = 0;
  for (let i = 0; i < 1000000; i++) {
    counter += i;
  }
  return counter;
}

console.time("FirstCalculation");
const memoizedExpensiveCalculation = memoize(expensiveCalculation());
console.timeEnd("FirstCalculation");

console.time("1_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("1_Memoized");

console.time("2_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("2_Memoized");

console.time("3_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("3_Memoized");

The time logs reveals, in fact, the first time takes much more time (which we would expect) and less time afterwards.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

it's not correct


The code you shows

function memoize(result) {
  let cache = {};

  return function() {
    if (cache[result]) {
      // returns cached result / no calculation
      return cache[result];
    }
    // calculating...
    cache[result] = result;
    return cache[result];
  };
}

is basically

function memoize(result) {
  return result
}

in memoization what you cache is input/output pair and skip real function call when input match.

about 4 years ago · Juan Pablo Isaza Denunciar

0

something like:

function get(func) {
  let cache = {};

  return function(key) {
    if (key in cache) {
      return cache[key];
    }
    cache[key] = func(key);
    return cache[key];
 };
}

function expensiveCalculation(param) {
  console.log('expensive calculation runs')
  let counter = 0;
  for (let i = 0; i < 10000; i++) {
    counter += i;
  }
  return param*2;
}
const memo = get(expensiveCalculation)
console.log('start')
console.log('first try');
console.log(memo(10));
console.log('second try');
console.log(memo(10));
about 4 years ago · Juan Pablo Isaza Denunciar
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