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

react-optimization-tools

v1.2.2

Published

Set of the fastest tools for optimizing the work of a React application

Downloads

49

Readme

react-optimization-tools

Set of the fastest tools for optimizing the work of a React application

Install

npm install --save react-optimization-tools

Features

The set includes eight algorithms:

four functions:

  • memoDeep

  • memoizeDeep

  • compareDeep

  • staticCallback

and four hooks:

  • useMemoDeep / useMemoDeepSE

  • useCallbackDeep / useCallbackDeepSE

  • useEffectDeep

  • useEvent

1. memoDeep

If the props are not changed, the component is not rendered. Analog React.memo but with deep comparison.

Example of use:

import React from 'react';
import { memoDeep } from 'react-optimization-tools';

const Component = (props) => {...};
export default memoDeep(Component);

2. memoizeDeep

Remembers the last result of the function. Analog memoize-one but with deep comparison and better performance. Based on project deepMemoizeOnce.

Example of use:

  • 2.1.
import { memoizeDeep } from 'react-optimization-tools';

const memoFunc = memoizeDeep((arg) => {...}); // heavy computation function

const mapStateToProps = ({ data }) => {
    return {
    propValue: memoFunc(data)
  }}
import { createSelectorCreator } from 'reselect';
import { memoizeDeep } from 'react-optimization-tools';

const customSelectorCreator = createSelectorCreator(memoizeDeep);
// or
const customSelectorCreator = createSelectorCreator(memoizeDeep, true); // clone the arguments of the function
// or
const customSelectorCreator = createSelectorCreator(memoizeDeep, { circular: true, strict: true }); // customizable cloning of the arguments of the function

const selector = customSelectorCreator(
  state => state.a,
  state => state.b,
  (a, b) => {...}
);

3. compareDeep

Function of fast deep comparison of two values.

Example of use:

import { createSelectorCreator, defaultMemoize } from 'reselect';
import { compareDeep } from 'react-optimization-tools';

const createDeepEqualSelector = createSelectorCreator(
  defaultMemoize,
  compareDeep
)

const mySelector = createDeepEqualSelector(
  state => state.a,
  values => {...}
)
import React from "react";
import { compareDeep } from "react-optimization-tools";

const MemoComponent = React.memo(Component, compareDeep);
// or
export default React.memo(Component, compareDeep);
import React from 'react';
import { compareDeep } from 'react-optimization-tools';

class Component extends React.Component {
  shouldComponentUpdate(nextProps) {
    return !compareDeep(this.props, nextProps);
  }
  render() {...}
}

4. useMemoDeep

Hook for memoizing the result of the function with unchanged values of the input parameters. Similar to useMemo, but with a deep comparison. The arguments of the hook: useMemoDeep(function, dependencies, isCloneProps = false)

Example of use:

import React, { useState } from 'react';
import { useMemoDeep } from 'react-optimization-tools';

const Component = () => {
const [ count, setCount ] = useState(0);

// ! attention is memoized result of the function !
const res = useMemoDeep(() => {...}, [count]);

return(...)
}

The documentation of the React says: "Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo." Like here:

const [count, setCount] = useState(0);

const value = useMemoDeep(() => {
  const res = count + 10;

  setState(res);

  return res;
}, [def]);

But if you need to use with side effects, then use the SE version - useMemoDeepSE

5. useCallbackDeep

Hook for memoizing a reference to a function with unchanged values of input parameters. Similar to useCallback, but with a deep comparison. The arguments of the hook: useCallbackDeep(function, dependencies, isCloneProps = false)

useCallbackDeepSE - version for side effects(explained above)

Example of use:

import React, { useState } from 'react';
import { useCallbackDeep } from 'react-optimization-tools';

const Component = () => {
const [ count, setCount ] = useState(0);

// ! attention is memoized function reference !
const funcRef = useCallbackDeep(() => {...}, [count]);

return(...)
}

6. useEffectDeep

Similar to useEffect, but with a deep comparison.

7. useEvent

The callback in the useEvent will always have access to the current state/props, while having a static link and without the transmission of dependencies.

Example of use:

const Component = () => {
  const [value, setValue] = useState(0);

  const onClick = useEvent(() => {
    console.log(value);
  });

  return (
    <div>
      <ChildComponent onClick={onClick} />;
    </div>
  );
}

8. staticCallback

Function for memoizing a reference to a function with unchanged values of input parameters. Similar to useCallbackDeep, but for class components. Used in cases when it is difficult to create functions in the main definition of the class, as in the example below. The arguments of the function: staticCallback(context, function, key, dependencies = [], isCloneProps = false)

Example of use:

import React, { PureComponent } from "react";
import { staticCallback } from "react-optimization-tools";
import GalleryItem from "./components/GalleryItem";

class Gallery extends PureComponent {
  sc = (fn, key, props) => staticCallback(this, fn, key, props);

  render() {
    const {
      sc,
      props: { items },
    } = this;
    return (
      <div>
        {items.map((props, index) => {
          const { hash } = props;

          return (
            <GalleryItem
              onDragStart={staticCallback(
                this,
                (event) => this.dragStart(event, index),
                `onDragStart${hash}`,
                [index]
              )}
              onDragEnd={staticCallback(
                this,
                (event) => this.dragEnd(event, index),
                `onDragEnd${hash}`,
                [index]
              )}
              // or
              onDragStart={sc(
                (event) => this.dragStart(event, index),
                `onDragStart${hash}`,
                [index]
              )}
              onDragEnd={sc(
                (event) => this.dragEnd(event, index),
                `onDragEnd${hash}`,
                [index]
              )}
              {...props}
            />
          );
        })}
      </div>
    );
  }
}

Benchmarks

The algorithms are based on the speed of the modified algorithm of the qcompare project. The modification was related to the ability to work with React components. Therefore, the performance of this algorithm was compared with similar projects. Performance tests were used from these projects.

Nodejs - v14.9.0
Date - 14.10.2020

react-fast-compare project test result

--- speed tests: generic usage ---
qcompare x 140,332 ops/sec ±0.44% (92 runs sampled)
react-fast-compare x 97,891 ops/sec ±1.27% (88 runs sampled)
fast-deep-equal x 94,725 ops/sec ±1.04% (90 runs sampled)
lodash.isEqual x 16,635 ops/sec ±24.04% (91 runs sampled)
nano-equal x 93,555 ops/sec ±0.27% (90 runs sampled)
shallow-equal-fuzzy x 54,279 ops/sec ±0.43% (89 runs sampled)
  fastest: qcompare

--- speed tests: generic and react ---
qcompare x 64,123 ops/sec ±0.59% (88 runs sampled)
react-fast-compare x 44,744 ops/sec ±0.70% (88 runs sampled)
fast-deep-equal x 33,168 ops/sec ±0.37% (89 runs sampled)
lodash.isEqual x 3,085 ops/sec ±10.04% (87 runs sampled)
  fastest: qcompare

License

MIT Yuriy Khomenko