Function-level Caching in JavaScript using Memoization

preview_player
Показать описание
In this video I demonstrate how to use the memoization pattern to implement a function level cache in javascript. To do that, the following language features of javascript are used:

*) Closures
*) Functions as parameters
*) Functions that return functions
Рекомендации по теме
Комментарии
Автор

Ah best explanation of this technique, the simple first example helped me finally understand what the computer is doing when it's making those recursive calls...

missizzyc
Автор

This is my FE engineer interview question, thanks for sharing!

raysunqi
Автор

This makes sense, but for those just learning this technique. Rather than using an array, it would make sense to use an object literal right? As the function being memoized can only ever have one output for the same input, using an object literal would ensure no duplicate keys are every possible and makes it more descriptive to the person reading the code. Also, in cases where the input is not a number, it a key property associated to the calculation would also be more flexible.

DanielRamBeats
Автор

For fib(n) memorization makes scense. But code generation and pre-calculation could also be a sollution for this example.

RogerKeulen
Автор

var cache =[] is setting empty every time here when function memorizer is called?

javascriptduniya
Автор

How to get execution time? I mean any in-built js function?

saikoushik
Автор

Hey, why do you turn n into a string? Is it strictly necessary?

kollerjon
Автор

here because i want a better understanding of how to use Apollo,

dantewhite
Автор

```
const memorizer = f => { .. .. .. .. .. .. };
const _slowFun = n => n+1;
const slowFun = memorizer(_slowFun};
export { slowFun };
```

xanaduzhang
Автор

```
const memorizer = (f, exp) => { if(exp).. .. .. .. .. .. };
const _slowFun = n => n+1;
const slowFun = (n, exp=false) => memorizer(_slowFun, exp} ;
export { slowFun };
```

xanaduzhang
visit shbcf.ru