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:
- Reusable memoisation — a decorated function is a function of one argument
- Function Cache — a very similar kata, but it has weaker tests
Trees
Similar Kata:
Stats:
Created | Oct 27, 2023 |
Published | Oct 28, 2023 |
Warriors Trained | 119 |
Total Skips | 32 |
Total Code Submissions | 151 |
Total Times Completed | 16 |
JavaScript Completions | 16 |
Total Stars | 5 |
% of votes with a positive feedback rating | 78% of 9 |
Total "Very Satisfied" Votes | 6 |
Total "Somewhat Satisfied" Votes | 2 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 10 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |