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-memoized

v1.0.2

Published

This package was develop to provide a more powerful hook than React's useMemo.

Downloads

21

Readme

Memoize

This package was develop to provide a more powerful hook than React's useMemo.

Why to use Memoize?

A problem of the useMemo is that it only holds the result of a function if the list of dependencies doesn't change between the re-renders, but if these values can repeat, the previous results calculated are forgetten, and the function will run again.

To solve this, i looked for ways to keep the old results also cached to be reused whenever the list of dependencies repeats, and based on the following article, I created a function that can be used EXACTLY the same way as a common hook, to that I could cache these responses and still keep the default hooks in the project.

Article: https://www.freecodecamp.org/news/understanding-memoize-in-javascript-51d07d19430e/

When use Memoize?

The useMemoized will cache all the results associated with the list of dependencies used during the life of the compoent, so if some dependency is random and can have unlimited values, you probably shouldn't use it.

The most appropriate use of useMemoized is when you have a limited set of values that cause a call of a function with a hight cost of time or processing, like a request to some api.

Example

There is a example using the useMemoized

import { useState } from 'react'
import useMemoized from 'use-memoized'

enum Period {
  DAY = 'day',
  WEEK = 'week',
  MONTH = 'month',
  YEAR = 'year'
}

function heavyFunction(period: Period) {
    // do heavy calcs
    return result
}

const Example = () => {
  const [period, setPeriod] = useState(Period.DAY)
  const result = useMemoized(() => heavyFunction(period), [period])

  return (
    <div>
        <select value={period} onChange={(e) => setPeriod(e.target.value as Period)}>
            <option value={Period.DAY}>1 Day</option>
            <option value={Period.WEEK}>1 Week</option>
            <option value={Period.MONTH}>1 Month</option>
            <option value={Period.YEAR}>1 Year</option>
        </select>
        <h1>{result}</h1>
    </div>
  );
};

export default Example

The heavyFunction will be executed at most 4 times, and so the result will be get from cache.