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

eslint-plugin-react-hooks-optimization

v1.0.3

Published

An ESLint plugin for React optimization suggestions like useMemo and useCallback.

Downloads

254

Readme

🚀 eslint-plugin-react-hooks-optimization 🔧

eslint-plugin-react-hooks-optimization is an ESLint plugin that recommends using useMemo and useCallback to optimize React projects and reduce unnecessary re-renders. This plugin detects performance issues in the codebase and provides specific recommendations to help developers achieve optimal performance.

✨ What's New in v1.0.3

  • 🎯 Precise Warning Locations: Warnings now point to the exact code location needing optimization
  • 🔍 Enhanced Loop Detection: Added detection for large iterations (e.g., i < 1000)
  • Performance Improvement: Operations inside Hooks are now recognized as already optimized

📦 Installation

npm install eslint-plugin-react-hooks-optimization --save-dev
# or
yarn add eslint-plugin-react-hooks-optimization --dev

⚙️ Configuration

Add to your ESLint configuration file (.eslintrc.json):

{
  "plugins": ["react-hooks-optimization"],
  "rules": {
    "react-hooks-optimization/prefer-optimization": [
      "warn",
      {
        "performanceThreshold": {
          "complexity": 5, // Function complexity threshold (number of conditions/loops)
          "arraySize": 1000 // Array size threshold (number of elements)
        }
      }
    ]
  }
}

Configuration Options Explained

  • complexity (default: 5)

    • Total number of conditions/loops in a function
    • Lower values suggest more aggressive optimization
  • arraySize (default: 1000)

    • Minimum array size requiring optimization
    • Lower values suggest more aggressive optimization

🎯 Memoization Rules

useMemo Suggestions

  1. Large Array Operations
// ⚠️ Warning
const largeArray = new Array(1000).fill(0);

// ✅ Recommended
const largeArray = useMemo(() => new Array(1000).fill(0), []);
  1. High Iteration Count
// ⚠️ Warning
for (let i = 0; i < 10000; i++) {
  // operations
}

// ✅ Recommended
const result = useMemo(() => {
  // iteration logic
}, []);

useCallback Suggestions

  1. Event Handler Props
// ⚠️ Warning: Functions starting with 'handle' or 'on' passed as props
const handleClick = () => {
  // handle logic
};
<ChildComponent onClick={handleClick} />;

// ✅ Recommended
const handleClick = useCallback(() => {
  // handle logic
}, []);

🤔 Why These Rules?

  1. Array Size (1000)

    • Large array operations can significantly impact render performance
    • The threshold of 1000 elements is based on common React application patterns
  2. Complexity (5)

    • Functions with 5+ conditions/loops often benefit from memoization
    • Helps prevent expensive recalculations during re-renders
  3. Event Handler Naming

    • Following React conventions for handler naming ('handle', 'on')
    • Ensures consistent optimization of component callbacks

🔧 Custom Configuration Examples

// Strict optimization
{
  "performanceThreshold": {
    "complexity": 3,
    "arraySize": 50
  }
}

// Default settings
{
  "performanceThreshold": {
    "complexity": 5,
    "arraySize": 100
  }
}

// Large-scale apps
{
  "performanceThreshold": {
    "complexity": 7,
    "arraySize": 500
  }
}

📜 License

MIT License

👨‍💻 Author

Created by Sijin