DEV Community

Discussion on: JavaScript Katas: Calculate total amount of points

Collapse
 
sh786 profile image
Sam Hamburger

Is there a benefit to memoizing the point addition of a specific game's score to avoid splitting, and comparing values, when we have already done so for the duplicate score?

I solved as shown below:

const calculateAmountOfPoints = games => {
  const seenScores = {};
  let total = 0;
  games.forEach(game => {
    if (seenScores[game]) {
      total += seenScores[game];
    } else {
      const [x, y] = game.split(":");
      let thisScore = 0;
      if (x > y) thisScore = 3;
      if (x === y) thisScore = 1;
      total += thisScore;
      seenScores[game] = thisScore;
    }
  });
  return total;
};
Collapse
 
miku86 profile image
miku86

Hey Sam,

great question, thanks!

If the performance of your application is a bottleneck, you could get some benefits through the memoizing.

There are probably already some optimizations going on under the hood of the JS engine, so I don't know about the magnitude of this manual memoizing.

If you know more about this topic or this specific case, please let us know.

Greetings
Michael