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

211
Views
¿Cómo evitar una llamada de función si se pasan los mismos argumentos?

Tengo el siguiente código y estoy tratando de evitar el cálculo dentro de complexFunction en caso de que se pasen los mismos argumentos.

 const complexFunction = (arg1, arg2) => { /* complex calculation in here */ console.log('complexFunction'); return true; }; // this is the method we need to implement const memo=()=>{ } const memoComplex = memo(complexFunction); memoComplex(1, 2); // complex function should be executed memoComplex(1, 2); // complex function should not be invoked again, instead the cached result should be returned memoComplex(1, 3); // complex function should be executed
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simplemente puede usar una matriz de objetos que contiene los argumentos para verificar si se han usado antes, el código a continuación demuestra el uso

 const usedArgs = []; const memoComplex = (arg1, arg2) => { /* complex calculation in here */ const value = arg1 + arg2; usedArgs.push({ arg1: arg1, arg2: arg2, data: value }); return value; }; const memo = (arg1, arg2) => { const exists = usedArgs.find(x => x.arg1 == arg1 && x.arg2 == arg2); if (exists) { console.log('loading from cache') return exists.data; } else return memoComplex(arg1, arg2); } console.log(memo(1, 2)); // complex function should be executed console.log(memo(1, 2)); // complex function should not be invoked again, instead the cached result should be returned console.log(memo(1, 3)); // complex function should be executed

about 4 years ago · Juan Pablo Isaza Report

0

Puede guardar los argumentos pasados y el resultado en el Almacenamiento de sesión y en la llamada al método, verifique si ya lo ha guardado en el almacenamiento, si ese es el caso, puede obtenerlo y devolver el resultado. De lo contrario, ejecutaría su método como siempre.

 sessionStorage.setItem('clé', 'valeur'); const complexFunction = (arg1, arg2) => { // Get item return null if not found let potentialResult = sessionStorage.getItem("#1" + arg1.toString() + "#2" + arg2.toString()); if(potentialResult !== null) { // If we have a result in session storage, that mean we have already done the calc // so we return the result return potentialResult; } else { // If we don't have a result, we just execute the function and save the result /* complex calculation in here */ let result = "Your result"; // Assuming result is the result of your function // We save the result to avoid future calculation sessionStorage.setItem("#1" + arg1.toString() + "#2" + arg2.toString(), result); console.log('complexFunction'); return true; } };

Pero como se dice en los comentarios, esto es más una pista que una solución , así que adáptelo a su caso de uso.

about 4 years ago · Juan Pablo Isaza Report
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!