npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-weak-map-memoizer

v1.0.0-alpha.0

Published

React-like interface for memoization without binding the order of calls. Based on WeakMap.

Downloads

2

Readme

🧐 useWeakMapMemoizer 🧐

Provides React-like interface for memorizing values and callbacks without binding the order of calls. Based on WeakMap.

Navigation

Installation

yarn add use-weak-map-memoizer
# or
npm install use-weak-map-memoizer 

Example

const MyListComponent = ({ list }) => {
  const wkmap = useWeakMapMemoizer(); // #1
  const selectItem = useCallback((item) => {/* some logic */}, [/* deps */])
  const addItem = useCallback((item) => {/* some logic */}, [/* deps */])
  
  return list.map(item => {
    const cache = wkmap(item); // #2
    const mappedItem = cache.memo(1, [], () => ({ ...item /*, some new fields */ })); // #3
    const onPress = cache.callback(2, [selectItem], () => selectItem(mappedItem)); // #4
    const onDoublePress = cache.callback(3, [addItem], () => addItem(mappedItem));
    
    return (
      <MyMemoizedItemComponent
        item={mappedItem}
        onPress={onPress}
        onDoublePress={onDoublePress}
      />
    )
  })
}

What's going on here?

  1. Initialize the memoizer
  2. Get the object for caching by reference key
  3. Similar to the working principle of useMemo.
    1. 1 is the caching key within the item object
    2. [] is an array of dependencies. It would be possible to omit this parameter
    3. () => {} — the creator function.
  4. The principle of operation is similar to p. 3, but only an analogy for useCallback

Use case

Mapping

It is convenient to memorize the results of mapping based on a reference.

const { list } = props;
const wkmap = useWeakMapMemoizer();

return list.map(item => {
  const mappedItem = wkmap(item).memo(() => ({...item, /*, some new fields */}))
  return <MyMemoizedComponent item={mappedItem} />
})

Closure

It is convenient to make closures in iterative components

const { list } = props;
const wkmap = useWeakMapMemoizer();
const addItem = useCallback((item) => {/* some logic */}, [/* deps */])

return list.map(item => {
  const onClick = wkmap(item).callback(1, [addItem], () => addItem(item))
  return <MyMemoizedComponent item={mappedItem} onClick={onClick}  />
})

You can come up with your own situations when it is convenient for you!

API

In short:

const wkmap = useWeakMapMemoizer(); // or createWeakMapMemoizer()

wkmap(obj).memo(creator)
wkmap(obj).memo(cacheKey, creator)
wkmap(obj).memo(cacheKey, deps, creator)

wkmap(obj).callback(callback)
wkmap(obj).callback(cacheKey, callback)
wkmap(obj).callback(cacheKey, deps, callback)

⚠️ By default, deps is an empty array.

That is, memoization will not compare dependencies and return a memorized result relative the reference object and cache key. This behavior differs from useMemo and useCallback

Important tips

Use cache key

If you need to memorize several results relative the same object, use the caching key.

const onPress = wkmap(obj).callback(1, [], () => { /* some logic */ }); // cacheKey is 1
const onDoublePress = wkmap(obj).callback(2, [], () => { /* some logic */ }); // cacheKey is 2

Only primitive types for cache key

You don't need to use reference types in caching keys, instead pass them to map(cacheKeyObj)

👨‍💻 Author

Sergey Rozhkov

🎯 Was it helpful?

Do you like it and find it helpful? You can help this project in the following way:

  • ⭐ Put the star.
  • 💡 Suggest your ideas.
  • 😉 Open a founded issue.

📄 License

useWeakMapMemoizer is MIT licensed, as found in the LICENSE file.