Using Memoization to Speed Up Code

# What is memoization?

Yeah, that's right, memo, not memor. I asked my self this when I first saw it. Simply put, memoization can be described as the caching of the results of a sub-step within an algorithm.

That makes it perfect for recursive functions, as they waste plenty of time recomputing everything, so that's what I tried it on, a recursive function.

And what recursive function is better known than a function to calculate the Fibonacci sequence?

After learning about memoization and how to do it, I spent hours perfecting a memoization wrapper which I then used on a JavaScript bigInt Fibonacci function.

I then laughed at the speed differences.

I calculated 1000 iterations of the Fibonacci sequence in 1 second. Without it, it took forever. You can try any number less than 200,000 without choosing to loop, and you'll receive an answer within 10 seconds, guaranteed or I'll refund you.

Try it out yourself!
https://MemoizedFibonacci.studentfires.repl.run

You are viewing a single comment. View All
bgrubert (177)

I put in 199,999 on the memoized option and it's taken longer than a minute - do I get my refund?

[deleted]

@bgrubert With or without the loop option? (I'm an associate of SF)

[deleted]

@bgrubert 200k was an estimate at when it crashes. It crashes on 199,999. But... I guess you deserve a full refund. How much did it cost you to push "Run"? (Now you see our loophole)

bgrubert (177)

@JadenGarcia It costed valuable food to give my finger the energy to press run

[deleted]

@bgrubert How much? How many milligrams? 1, 2?

DynamicSquid (4544)

@bgrubert, Well, according to this, the average "flick" wastes 0.15J of energy. So you burned 0.04 calories. Which means, @JadenGarcia, you owe @bgrubert 1/2375 of an apple xD

bgrubert (177)

@DynamicSquid So if the apple's price was \$.44, @StudentFires would owe me 0.018 cents

xxpertHacker (617)

@bgrubert Yes, as my associate wrote, when the global pandemic is over, we will refund you in full. As of right now, any more runs will no longer be refunded. In order to refund you in full, we need to request that you provide us with the following information: to know where to send your refund: the planet where you're home is located (if applicable), the country (if applicable), the state (if applicable), providence (if applicable), region (if applicable), city / town, (if applicable), street (if applicable), mailbox (if applicable), home address (if applicable), and, preferably, some details about the external part of your house (if applicable) that distinguishes it from any nearby houses (if applicable). If none of the information requested is applicable to your current situation, please provide an address where you would like us to send your refund. And once again, this will only happen if and when the quarantines end, in both, the city that © StudentFires is located, and in the requested delivery area.

xxpertHacker (617)

@DynamicSquid ©StudentFires will be taking full responsibility for refunding @bgrubert. @JadenGarcia is in no way whatsoever involved in the refund guarantee, other than that he is indeed an associate of ©SF.

Also, totally unrelated, but can you give us an upvote on the post?

xxpertHacker (617)

@bgrubert Our email, or what method did you use to inform us?

xxpertHacker (617)

@bgrubert Oh, the upvote! The good folks at StudentFires thank you!

xxpertHacker (617)

@DynamicSquid You're assuming he even eats apples. This may not entirely be true, instead he may eat much more (or less) expensive food, depending on his lifestyle.
It is uncommon for someone to be a vegetarian, much less a vegan, so he may have eaten some expensive steak, or simply a McDonald's burger. He may have eaten a can of beans, he may have sat around eating chips, we cannot simply assume such events occurred (or didn't).

DynamicSquid (4544)

@StudentFires lol, also, why do you unvote your own posts?

xxpertHacker (617)

@DynamicSquid Psychology? You see someone who has 0 votes, don't you want to give them a vote?

DynamicSquid (4544)

@StudentFires Interesting... I've never thought of it that way, you know, I'm going to give it a try and see how it goes. I'll let you know next week if it works

xxpertHacker (617)

@DynamicSquid Where better to start than right now, here?

xxpertHacker (617)

@DynamicSquid See! I just had to give you that upvote!

firefish (885)

@bgrubert Wow, have your 2 cents and leave, keep the change

bgrubert (177)

@firefish It's actually less than 2% of a cent lol

firefish (885)

@bgrubert oh wait... I though he said 0.018 dollars...
then take your cent, keep the change

xxpertHacker (617)

@firefish It's less than half of a cent; we round down :)

firefish (885)

@xxpertHacker Take your air, keep the change.

firefish (885)

@xxpertHacker It's so cheap in the weakest currency in the world as well

xxpertHacker (617)

@firefish I almost want to say that you can't legally own air, but I guess people have canisters of gas, so I guess it is.

Well, at least no one will argue that I need to refund them for the amount of energy it took them to press a button.

bgrubert (177)

@firefish Yeah it's about the price of 1 cup of tap water

firefish (885)

@bgrubert Wow your water is cheap