Memoize function in coordinate transformation exercise

Hello.

I’m stuck again but now with the memoization thing.
My code is failing test 15: memoizeTransform > should not call the memoized function if the input is the same
But once again I can’t see why because as far as my understanding goes, it’s pretty clear the memoized function f is not called if you provide the same input twice because it’s only called in the body of the if statement when the input is new.

What’s going on?

Thank you.

export function memoizeTransform(f) {
  const cache = {
    lastInput: null,
    lastResult: null
  }; // for storing already computed data.

  function applyFunction(a, b) {

    if (cache.lastInput !== [a, b]) {      
      cache.lastInput = [a, b];      
      cache.lastResult = f(a, b);
    }
    return cache.lastResult;
 }
  
  return applyFunction;
}

Actually it is (called twice). Do some googling on array comparison.

You cannot compare two arrays using if-statements, since arrays are Objects and are not compared based on values, the memoize function will always experience a cache miss.

You can test this by executing [] == [] and checking the return evaluation.

2 Likes

Thank you so much.
That’s what I was missing.

I leave the following article for reference purposes:

1 Like