Memoization in Javascript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends so tonight we're going to do let's make a quick video on memolization in javascript now this can be a kind of confusing subject for people um there's really not much to it though people like to make uh make things sound more difficult than they actually are but if you understand the underlying principle about and if you're taught it in such a way to where it's easy to understand and you see the use case for it then it's a lot there's a lot more utility in that i find so what is memoization uh people might get it confused with memorization but uh memoization it sounds like a baby's trying to say memorization memoization okay that's enough of that okay so minimalization is basically caching return values of a function based on specific parameters well why would you want to do that that sounds just like a word salad doesn't really seem to says it's a lot of words without saying anything really so the idea is that you have a function that maybe takes a long time to run but it might run the same output several times so you'll want to cache those outputs so you can get beyond running it based on the parameters let's take a look at an example so let's take a very easy example that will illustrate the utility in learning this so let's say we have a function called multiply mult by 2 and this function is going to take in a parameter called num and so what we want to do here is we just want to return the number multiplied by 2. that's what the function does however let's console log right here long wait time to signify that that this is how a function work if the function takes a long time to actually to actually come from being invoked to outputting a return value there might be a long wait time in there so we're just going to console log long wait time and we're going to try to get around the long wait time basically so we don't want the long wait time is what i'm saying so here if we run this like normally we'll just go console.log whoops clg mult by 2 and we'll just return 2 times 2 is 4. so if we run that in node node main dot js you see that we get a long wait time and then finally it outputs 4. so how can we get around this long wait time well let's say that we were doing that same calculation again down the line but in between we had like we were going to console.log it with 3 right here and then here we're going to do it with 4. so basically every time we run mult multiply by two with two as an argument we already have the answer we know that it's four so if we can have a caching system we can get around that long wait time right here if we just cash out this value here so when it runs again here and then again here on line 13 and then again here on live 14 we should theoretically be able to get around this console log long wait time which is what we want to do so let's uh let's see what happens here so you see how on this every time you're getting a long wait time before the output we want to try to get around that so let's erase these let's make ourselves some room to work so here we'll make another function function and this one's called memo oi's mult by 2. so we want to actually memoize this function right here so the way that you do it is you're going to make a cache which should just be a javascript object that's a good way to do it so we go const cache right here equals an empty object then what you want to do is actually return a function with the parameter in that function so we'll actually return an anonymous function so return function and this is where the parameter will go of num it'll go here and then what we want to do is check so we'll go if uh num in cash so what num in cash does is it checks to see if this argument is present as a key in this cache if it is we just want to return cache at num this will give us the value of the num key in the object that's how you access that else what we want to do is we want to cache it so go cache at num is going to be equal to num times 2 this is where we'll do our initial calculation then we'll console.log long wait time and then we'll return cash at num right so basically each time that a new argument is entered here each time that there's a new argument we're going to hit this long wait time because we're going to be caching the output however if we run that same output again we'll get around the long wait time because it'll just already be in cash right here and you know that you have access to this cache on line 10 within the function body right here in this return function because of closure which i made a video on the other day so you know that this function body right here it has access to variables that lie outside of its scope uh going toward the glo going toward the global scope because of closure so even if we put cash out here it would still have access to it but cash being out there is fairly useless to us so just know that the return function has access to the cache object so now let's let's do it like this let's console log well first we have to do okay here's another thing i forgot to tell y'all so basically since we're middlewise mult by 2 is going to return a function right here right so how do we get access to this function are we going to do like some function currying to where we can we go maybe memoize mem ois mult by 2 and then we invoke it again and we put the number in here technically you you could do that but that's not the best way to do it the best way to do it is just to make a variable so we'll go const memoize is gonna equal memoize mult by two invoked so if we console.log memoize right now you're gonna see that it's just gonna return that dormant function that hasn't been invoked yet so what we want to do to invoke it is we want to pass in a number right here so let's pass in 2 right here and again the first time that we run it we're going to get the long wait time so if we run it we'll get long wait time and then 4 but here's the thing if we run it again we will get around that long wait time and it will just it'll just return the four so you can see the initial time it returns long wait time just like up here it caches the number then it returns long wait time signifying the long wait time that it takes for the function to run then it returns the output from the cash other if the cash is being read then the only thing that's happening now is that we're returning the cash so we're getting around all of this work right here so we're making our function a lot more performant so if we have let's do a bunch of these so if we do this this this this and this if we had three right here oops if we had three right here four right here two right here and three right here or four right there how many of these would would get the long wait time well really it would only be one two three things that are getting along wait time the rest of them will get around that so if you see right here we get long wait time by four now four is cash so it's just gonna return four long wait time cashing six so now anytime six is ran again it or i'm sorry anytime three is ran again it's going to return long wait time for four times two is eight so four anytime four is again it's going to go in there it's going to be cash right here so that's the utility in in learning minimalization basically anytime that you can cash out your outputs based on the parameter you can make your function more performant now you're going to have to remember that you're going to be returning a function here so you'll have to set it up in a variable like this const memoize equals memoize multiply two invoked equals this function then you can run this function with the argument there rather than just returning a function like this which is always going to get the long wait time and be not that performant so that's memoization in a nutshell hope you enjoyed it hope it helped take it sleazy
Info
Channel: Adam Coder
Views: 817
Rating: 5 out of 5
Keywords:
Id: mKNtUsGrV0w
Channel Id: undefined
Length: 8min 23sec (503 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.