Code Monkey home page Code Monkey logo

re-reselect-flat-weak-map-cache's Introduction

Re-reselect FlatWeakMapCache Object

Custom cache object to be used with Re-reselect. It utilizes a Weak Map so that both the cache key object and selectorFn can be garbage collected.

Usage

import createCachedSelector from 're-reselect';
import { FlatWeakMapCache } from 're-reselect-flat-weak-map-cache';

createCachedSelector()({
  // ...
  keySelector,
  cacheObject: new FlatWeakMapCache()
});

See example for full usage.

Motivation

By default, only allows string or number cache keys. It does provide a few custom cache objects that will allow any type as a cache key. These objects, like FlatMapCache, utilize a Map.

The downside with using a Map is that any object you use as a key and its associated selectorFn will not be garbage collected. Even if the object used as a key is no longer referenced in the application.

While this likely won't be an issue for most applications, the associated memory leaks could be a problem for memory intensive applications.

I've provided an example to illustrate a use case you've probably seen in Redux applications.

To follow best practices, objects are treated as immutable: any change to an object should yield a new object, not mutate the original. If that object is used as a cache key, each iteration of the object will not be able to be garbage collected. And each selectorFn associated with that key will not be garbage collected.

Conversely, a weak map holds a weak reference to the key object. So if there are no other references to the object, it and its value will be garbage collected.

Contributors

TODO

  • Publish to NPM
  • Add another cache object that will handle a key that can be a primitive type as well (weak maps don't allow primitive types)
  • This is my first NPM package, and I may not have followed best practices, so I plan to follow this tutorial to improve this package structure
  • More descriptive errors when attempting to use a primitive type as a key

References

re-reselect-flat-weak-map-cache's People

Contributors

byanofsky avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.