-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemoize.js
51 lines (43 loc) · 1.38 KB
/
memoize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
PROBLEM
Given a function fn, return a memoized version of that function.
A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.
fn can be any function and there are no constraints on what type of values it accepts. Inputs are considered identical if they are === to each other. in js
*/
// SOLUTION
const memoizeFn = (fn) => {
// create a cache
let cache = new Map()
// return a function with arguments
return (...args) => {
// create a key
const key = JSON.stringify(args)
// check if there is no cache using the key
if (!cache.has(key)) {
// call the original function and store the result in the cache
cache.set(key, fn(...args))
}
// return the value from the cache
return cache.get(key);
}
}
const memoi = function (fn) {
console.assert(typeof fn === 'function', "Input should be a function")
let cache = new Map()
return (...args) => {
const key = JSON.stringify(args)
if(cache.has(key)) {
return cache.get(key)
}
else {
let res = fn(...args)
cache.set(key, res)
return res
}
}
}
const res = function (x, y) {
return x + y
}
const memoizedHost = memoizeFn(res)
console.log(memoizedHost(2,6))