{"id":7744,"date":"2023-06-08T10:53:18","date_gmt":"2023-06-08T05:23:18","guid":{"rendered":"https:\/\/innovationm.co\/?p=7744"},"modified":"2023-06-08T10:53:18","modified_gmt":"2023-06-08T05:23:18","slug":"memoization-in-javascript","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/","title":{"rendered":"Memoization in Javascript"},"content":{"rendered":"<p>Memoization is a powerful technique used in computer science to optimize the<br \/>\nperformance of functions that are called repeatedly with the same arguments. In<br \/>\nJavaScript, memoization is particularly useful for expensive computations that<br \/>\ninvolve complex algorithms or data structures.<\/p>\n<p>At its core, memoization involves caching the result of a function based on its<br \/>\ninput arguments. If the function is called again with the same arguments, the<br \/>\ncached result is returned instead of recomputing the result from scratch. This can<br \/>\nsignificantly improve performance and reduce the amount of time and resources<br \/>\nrequired to execute the function.<\/p>\n<p>To implement memoization in Javascript, there are a few different approaches you<br \/>\ncan take. One common technique is to use a simple object to store the cached<br \/>\nresults.<\/p>\n<p><strong>Here\u2019s an example:<\/strong><\/p>\n<pre>\r\nfunction memoize(func) {\r\nconst cache = {};\r\nreturn function(num) {\r\nif (cache[num]) {\r\nreturn cache[num];\r\n} else {\r\nconst result = func(num);\r\ncache[num] = result;\r\nreturn result;\r\n}\r\n};\r\n}<\/pre>\n<p>In this example, we define a higher-order function \u2018memoize\u2019 that takes a function<br \/>\n\u2018func\u2019 as its argument. The \u2018memoize\u2019 function returns a new function that wraps<br \/>\nthe original \u2018func\u2019 function.<\/p>\n<p>The wrapper function uses a cache object to store the results of previous<br \/>\nfunction calls. Each time the wrapped function is called with a new set of<\/p>\n<p>arguments, it checks whether the result has already been cached. If it has, the<br \/>\ncached result is returned. If not, the wrapped function calls the original \u2018func\u2019<br \/>\nfunction with the arguments caches the result and returns it.<\/p>\n<p>Here\u2019s an example of how can use the memoize function to optimize a function<br \/>\nthat calculates Fibonacci numbers:<\/p>\n<pre>function fibonacci(n) {\r\nif (n &amp;lt;= 1) return 1;\r\nreturn fibonacci(n - 1) + fibonacci(n - 2);\r\n}\r\nconst memoizedFibonacci = memoize(fibonacci);\r\nconsole.log(memoizedFibonacci(40)); \/\/ returns 165580141<\/pre>\n<p>In this example, we define a function \u2018Fibonacci\u2019 that recursively calculates<br \/>\nFibonacci numbers. We then use the \u2018memoize\u2019 function to create a new function<br \/>\n\u2018memoizedFibonacci\u2019 that caches the result of previous function calls.<\/p>\n<p>When we call the \u2018\u2018memoizedFibonacci(40)\u2019\u2019, the results are returned almost<br \/>\ninstantaneously because the result has already been cached from previous<br \/>\nfunction calls. Without memoization, calculating the 40th Fibonacci number<br \/>\nwould take several seconds or even minutes.<\/p>\n<p>In conclusion, memoization is a powerful technique for optimizing the<br \/>\nperformance of functions that are called repeatedly with the same arguments. In<br \/>\nJavaScript, memoization can be implemented using a simple object to store<br \/>\ncached results. By using memoization, you can significantly improve the<br \/>\nperformance of your code and reduce the amount of time and resources required<br \/>\nto execute complex algorithms or data structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Memoization is a powerful technique used in computer science to optimize the performance of functions that are called repeatedly with the same arguments. In JavaScript, memoization is particularly useful for expensive computations that involve complex algorithms or data structures. At its core, memoization involves caching the result of a function based on its input arguments. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7745,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256,360],"tags":[722,224,346,877],"class_list":["post-7744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-application","category-javascript","tag-blog","tag-java","tag-javascript","tag-memoization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Memoization in Javascript - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Memoization in Javascript - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Memoization is a powerful technique used in computer science to optimize the performance of functions that are called repeatedly with the same arguments. In JavaScript, memoization is particularly useful for expensive computations that involve complex algorithms or data structures. At its core, memoization involves caching the result of a function based on its input arguments. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-08T05:23:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2023\/06\/Memoization-in-Javascript.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1689\" \/>\n\t<meta property=\"og:image:height\" content=\"950\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Memoization in Javascript\",\"datePublished\":\"2023-06-08T05:23:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/\"},\"wordCount\":375,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/Memoization-in-Javascript.png\",\"keywords\":[\"blog\",\"java\",\"JavaScript\",\"memoization\"],\"articleSection\":[\"Java Application\",\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/\",\"name\":\"Memoization in Javascript - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/Memoization-in-Javascript.png\",\"datePublished\":\"2023-06-08T05:23:18+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/Memoization-in-Javascript.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/Memoization-in-Javascript.png\",\"width\":1689,\"height\":950},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/memoization-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Memoization in Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Memoization in Javascript - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Memoization in Javascript - InnovationM - Blog","og_description":"Memoization is a powerful technique used in computer science to optimize the performance of functions that are called repeatedly with the same arguments. In JavaScript, memoization is particularly useful for expensive computations that involve complex algorithms or data structures. At its core, memoization involves caching the result of a function based on its input arguments. [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/","og_site_name":"InnovationM - Blog","article_published_time":"2023-06-08T05:23:18+00:00","og_image":[{"width":1689,"height":950,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2023\/06\/Memoization-in-Javascript.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Memoization in Javascript","datePublished":"2023-06-08T05:23:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/"},"wordCount":375,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2023\/06\/Memoization-in-Javascript.png","keywords":["blog","java","JavaScript","memoization"],"articleSection":["Java Application","JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/","url":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/","name":"Memoization in Javascript - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2023\/06\/Memoization-in-Javascript.png","datePublished":"2023-06-08T05:23:18+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2023\/06\/Memoization-in-Javascript.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2023\/06\/Memoization-in-Javascript.png","width":1689,"height":950},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/memoization-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Memoization in Javascript"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/7744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=7744"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/7744\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/7745"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=7744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=7744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=7744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}