Skip to content

Commit 55e34d2

Browse files
authored
Update js codes.docx
1 parent cff8a57 commit 55e34d2

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

‎js codes.docx‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,4 +2874,66 @@ How to Remove All Falsy Values from an Array in JavaScript:
28742874
//This works because Boolean itself is a function, and the arguments filter supplies are passed directly to it.
28752875
//Boolean() is also a function that returns truthy when true and falsy when false!
28762876
========================================================================================================================================================================
2877+
Memo Function:
28772878

2879+
********************Memoizing functions**********************
2880+
2881+
const add = (n) => (n + 10);
2882+
add(9);
2883+
// a simple memoized function to add something
2884+
const memoizedAdd = () => {
2885+
let cache = {};
2886+
return (n) => {
2887+
if (n in cache) {
2888+
console.log('Fetching from cache');
2889+
return cache[n];
2890+
}
2891+
else {
2892+
console.log('Calculating result');
2893+
let result = n + 10;
2894+
cache[n] = result;
2895+
return result;
2896+
}
2897+
}
2898+
}
2899+
// returned function from memoizedAdd
2900+
const newAdd = memoizedAdd();
2901+
console.time(); console.log(newAdd(9)); console.timeEnd();// calculated
2902+
console.time(); console.log(newAdd(9)); console.timeEnd(); // cached
2903+
console.time(); console.log(newAdd(9)); console.timeEnd(); // cached
2904+
//output:
2905+
//Calculating result 19 default: 0.38427734375 ms
2906+
//Fetching from cache 19 default: 0.30810546875 ms
2907+
//Fetching from cache 19 default: 0.27612304687 ms
2908+
2909+
********************Memoizing recursive functions**********************
2910+
2911+
// a simple pure function to get a value adding 10
2912+
const add = (n) => (n + 10);
2913+
// a simple memoize function that takes in a function and returns a memoized function
2914+
const memoize = (fn) => {
2915+
let cache = {};
2916+
return (...args) => {
2917+
let n = args[0]; // just taking one argument here
2918+
if (n in cache) {
2919+
console.log('Fetching from cache');
2920+
return cache[n];
2921+
}
2922+
else {
2923+
console.log('Calculating result');
2924+
let result = fn(n);
2925+
cache[n] = result;
2926+
return result;
2927+
}
2928+
}
2929+
}
2930+
// creating a memoized function for the 'add' pure function
2931+
const memoizedAdd = memoize(add);
2932+
console.time(); console.log(memoizedAdd(3)); console.timeEnd();// calculated
2933+
console.time(); console.log(memoizedAdd(3)); console.timeEnd(); // cached
2934+
console.time(); console.log(memoizedAdd(3)); console.timeEnd(); // cached
2935+
//output:
2936+
//Calculating result 13 default: 0.45068359375 ms
2937+
//Fetching from cache 13 default: 0.16674804687 ms
2938+
//Fetching from cache 13 default: 0.161865234375 ms
2939+
========================================================================================================================================================================

0 commit comments

Comments
 (0)