Beta

Multiple Arguments Memoization

Description:

Let's assume we have a slow pure function.

function double(x) {
  for(let i = 0; i < 1e9; i++);
  return x * 2;
}

double(10) === 20 // but it takes ≈1s

Your task is to implement the memo decorator. This function accepts one argument fn and returns a new fuction which does the same work but doesn't compute a result for the same arguments twice.

const mDouble = memo(double);

mDouble(1) === 2 // ≈1s
mDouble(1) === 2 // ≈0.001s
mDouble(3) === 6 // ≈1s
mDouble(1) === 2 // ≈0.001s
mDouble(3) === 6 // ≈0.001s

The fn should work with any type and any length of function arguments. For example, the arguments can be numbers, bigints, arrays, objects or functions.

Similar katas:

Trees

Stats:

CreatedOct 27, 2023
PublishedOct 28, 2023
Warriors Trained119
Total Skips32
Total Code Submissions151
Total Times Completed16
JavaScript Completions16
Total Stars5
% of votes with a positive feedback rating78% of 9
Total "Very Satisfied" Votes6
Total "Somewhat Satisfied" Votes2
Total "Not Satisfied" Votes1
Total Rank Assessments10
Average Assessed Rank
6 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • maxsinyakov Avatar
Ad