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

shallow-element-equals

v1.0.1

Published

Efficient shallow equality algorithm that also allows checks for react element equality of children props

Downloads

6,718

Readme

shallow-element-equals

Efficient shallow equality algorithm that also allows checks for react element equality of children props

Why

shouldComponentUpdate is a powerful way to improve performance of react and react native applications, but often you have components which you can expect to be "pure", but you also want them to have an API that accepts children.

Having a children prop pretty much removes any chance of using a "shallow" equality comparison of props, since React.createElement will return a new object reference on every call, so JSX elements are always new object references.

shallowElementEquals takes this into account, and treats children props in a special way such that it will assume that all of the children elements provided to a component are "pure" as well, and just the props/types could be compared for an optimized comparison.

Be careful using this

This is dangerous. Don't use this function if you don't understand its consequences. By having a component adopt a shouldComponentUpdate method like this, you are assuming something about the components that people are passing into your component as children that may not be true (ie, that they are pure). If this is not true, the consumers of your component may have their application behave in ways that they do not expect, and the reason will be completely opaque to them.

I would probably not recommend using this type of an optimization on public code or open source projects where lots of people will be using it without understanding these assumptions.

Installation

npm i shallow-element-equals --save

Usage

import shallowElementEquals from 'shallow-element-equals';

// ...

shouldComponentUpdate(nextProps) {
  return !shallowElementEquals(this.props, nextProps);
}

Examples of how this works

See the tests to understand better what this will match on.