Javascript Memoization Magic

Javascript Memoization Magic

?? Closures VS Prototype-based Inheritance in Caching

Hey there Devs,

JavaScript’s flexibility shines through in how it handles memoization, caching expensive function results for more efficiency. Today I felt good, so I thought why not break down their differences for you and explore together how closures and prototype-based inheritance differ in implementing this powerful optimization technique.

With closures, we are actually taking advantage of JavaScript’s beautiful combination of a function and the lexical environment (within which a function is defined, determining what variables and parameters it has access to, in this case, its outer functions) to encapsulate the cache within the function scope, ensuring privacy and efficient memory usage. Here’s a simple example of memoizing a factorial function using closures:

excuse my simple example, was the first to think of :)

on the other hand with Prototype-based Inheritance, we're leveraging true nature of functions being just High-class Objects (AKA first class citizens).if you didn't know in short this allows us attach properties directly to functions or their prototype. Alternatively, using prototype-based inheritance allows us to attach a cache directly as a property of a function object. This approach provides visibility and accessibility to the cache, making it ideal for scenarios where shared state is advantageous:

Differences ???

Visibility of Cache: Function properties store the cache as a property of the function, making it accessible for debugging but exposing internal details. Closures store the cache in a variable within the closure, making it private and inaccessible from outside, ensuring encapsulation.

Initialization: Function properties require the cache to be explicitly initialized, which can be forgotten and cause errors. Closures automatically initialize the cache within the function’s scope, ensuring proper setup.

Multiple Instances: Function properties use a single shared cache for all function calls, which is problematic for different contexts. Closures create a new instance with its own cache for each function call, useful for isolated contexts.

Memory Management: Function properties keep the cache in memory as long as the function exists, risking memory leaks. Closures manage the cache within the closure, allowing for garbage collection once the closure goes out of scope, enhancing memory management.

When to Use Which Then ?

  • Use Function Properties when:

? You need to share the cache across different instances or contexts.

? You want easy access to the cache for debugging or inspection.

? You want to quickly add memoization to an existing function.

  • Use Closures when:

? You need encapsulation and privacy for the cache.

? You require multiple instances with separate caches.

? You want better control over the initialization and lifecycle of the cache.

of course the use cases for these 2 elegant javascript feature will not end here. there are lot's of other practical use cases including items below but i'll leave discovering their implementation details to you and your curiosity!

  • Creating Reusable & Flexible Mixing (ex: CRUD Operations)

  • Enhancing Functions With logging or Timing

  • Event Handling in Custom Objects

  • Chaining Methods in Fluent Interfaces

  • Dynamic Object Factories

Hope you all found it useful!

要查看或添加评论,请登录

Hooman Pouyanasab的更多文章

  • Incremental Hydration Mechanism Simplified

    Incremental Hydration Mechanism Simplified

    ?? First, let’s ask the main question: What makes a website slow? ?? Correct, JavaScript. ?? Long story short, it’s…

    1 条评论
  • NgRx v18 Signals (Part 1): Signal State

    NgRx v18 Signals (Part 1): Signal State

    ?? Angular has been evolving quite a lot recently and with the arrival of signal-based components it will be completely…

    1 条评论

社区洞察

其他会员也浏览了