0% found this document useful (0 votes)
6 views10 pages

Memorization in JS

This Pdf includes topics memorization of that is caching data that allready solved using it I for next calculation and reducing, calculation time and memory consumption.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Memorization in JS

This Pdf includes topics memorization of that is caching data that allready solved using it I for next calculation and reducing, calculation time and memory consumption.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Save it Like it

JS
Cache function result
using Memoization

@coder_aishya
01

Flow using memoization

Input Output

Memonizer function

Check in cache if present in cache return


object, the cache value stored in
whether same cache else compute the
request is passed ? result

@coder_aishya
02

What is Memoization ?
Memoization is an optimization technique
where expensive function calls are
cached(stored) such that the result can be
immediately returned the next time the
function is called with the same arguments

Expensive function calls


Time and memory are the two most
important resources in computer
applications. As a result, an expensive
function call is one that consumes large
amounts of these two resources due to
extensive calculation during execution.

@coder_aishya
03

Cache
A cache is just a temporary data store(JS
object) that stores data in order to serve
future requests for that data more quickly.
Note: Browser cache need not be applied in
this scenario.

How does it work?


If the input has been used before, locate it
in cache and immediately return the value
stored without executing any further
computations.
Use the input to execute any necessary
computations, store the results in cache,
return the result

@coder_aishya
04

Steps:
define a function using ES6

declare an initial cache object

@coder_aishya
05

Add logic : If the input is present in cache


then return it from cache without computing
the same request again else do the
computation

@coder_aishya
06

Example
Create a memoize function that remembers
previous inputs and stores them in cache so
that it won’t have to compute the same
inputs more than once. The function will take
an unspecified number of integer inputs and
a reducer method.

@coder_aishya
07

Memonizer function

@coder_aishya
08

Executing the function

The below statement returns output 10 from


cache since its called for the second time

@coder_aishya
Follow me for more

aishwarya-dhuri

coder_aishya

You might also like