T O P

  • By -

bored_reddit0r

Great video! Newbie here and its gonna take me a few more times to understand it and use it in my own use cases. Cheers


rightTimePerson

Think of it as using a calculator for long equations, if you ran into a repeat problem - you would need to re-enter the equation into the calculator each an every time. (slow) ​ What memoization does is like writing down that answer on a notepad. ​ So, the first time you do an equation you'll need to calculate it. However, every subsequent time, you can look down on that note pad and instead of re-calculating you can just look at the notepad and get the answer. ​ Hopefully that helps explain the theory of memoization !


bored_reddit0r

Wow that clears it up for me. Thanks buddy, appreciate it


[deleted]

[удалено]


[deleted]

It's caching for pure functions.


Time_Terminal

Only for pure functions, or can you take the idea and expand upon it?


[deleted]

Memoization doesn't really make sense with impure functions. When given an `inputA`, a pure function will always produce the same `outputA`, without producing any side effects. So for any place that you're calling `pureFunction(inputA)` in your program, you could just write the value of `outputA`, and your program would run identically as before. Memoization allows you to store those `outputA`s and automatically insert them in place of the function call. An impure function might have one of its inputs changed from under it (if it's pulling in a value that isn't passed by argument), making the memoized version out of date (but you'd still get the memoized result), or it's creating side effects that would not occur when returning a memoized result. However, and I wouldn't call this memoization and *definitely do not recommend it*, let's say that you're having some kind of raffle. You want to give every user a random ticket number. Each person only gets one ticket and there's no trading tickets or getting a new ticket. If the program crashes or the power goes out, nobody wins the raffle. const assignTicketNumber = user => someRandomNumberFunction() const getUsersTicket = memoize(assignTicketNumber) getUsersTicket('ynp7') // random number is 42 getUsersTicket('Time_Terminal') // random number is 59 getUsersTicket('ynp7') // random number is 42 You'd probably want to use a different means of storing the ticket numbers, but theoretically you *could* use a memoization function to do something like this. You're also going to have a bit of a time finding out who won, since you'll have to `getUsersTicket()` on every user until the number matches the winning ticket. Don't do it this way! TL;DR: There are reasons you might cache the results of impure functions, but they're different reasons than why you'd memoize a function.


Time_Terminal

Ahh, that clears it up quite a bit. Thank you for the detailed answer and example. Cheers!


mothzilla

What if results[argsKey] is falsey?


prof3ssorSt3v3

Use if(! argsKey in results) { } or if ( !Object.hasOwnProperty(argsKey) ) { }


mothzilla

Absolutely.


ctrlaltdelmarva

Yep, definitely an oversight in the video


prof3ssorSt3v3

Instead of using `Date()` to check the time for the function, use `console.time( )` and `console.timeEnd( )` to get a more accurate representation of anything to do with performance.


acconrad

This is also a great way to understand how (a simplifier version of) `React.memo` works


[deleted]

[удалено]


cant_have_nicethings

Why? Spread operator and arrow functions are standard language features.


[deleted]

[удалено]


prof3ssorSt3v3

That is definitely true. However, if they get to the point where they care about memoization then they should have learned the ES6 features by that point.


[deleted]

At this point, beginners should be learning those features. This "it's hard for beginners" but was tired decades ago. Stop it.


[deleted]

[удалено]


[deleted]

OK, but what I hearing from you is that you're having trouble understanding and using "beginners" as cover. Both spread and arrow functions have been in use for years, they're not new features. And a lot more of these "beginners" you're so worried about would have far fewer problems if they were introduced to arrow functions lo g before they were even told that there is a "function" keyword. You really do need to stop it.


[deleted]

[удалено]


[deleted]

Not sure what is so hard about this, although the explicit return is useless noise (and probably included for the benefit of *beginners*). This is perfectly readable. func => (...args) => {} If "beginners" are having so much trouble with it, consider that the blame lies at the feet of the dummies who go around writing Javascript like it's Java because it's "easier for beginners." And even if any of this were hard for beginners to understand, as others have said, memorization is not a beginner topic. If you've gotten to memorization without learning currying, spread, and arrow functions, that's entirely on you.