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

deep-diff-object

v1.1.0

Published

The purpose of this package is to deep diff two objects. This will, compare two objects and then output an object with all the changes between them.

Downloads

209

Readme

Object diffing

Philosophy

The purpose of this package is to get the deep diff two objects. It will compare two objects and then output an object with all the changes between them.

The original intent was to use it with React (see bottom of the readme) but it was made to be used solo.

Usage

Add it to your project

npm i -D deep-diff-object

Import it on your component

import deepDiff from 'deep-diff-object'

How to use it

const result = deepDiff(ObjectOne, ObjectTwo);

The 'result' object will look like this:

result = {
  one: [
    {'key1': value1},
    {'key2': value2},
    ...
  ],
  two: [
    {'key1': value1},
    {'key2': value2},
    ...
  ],
  ...
};

Whereas 'key1' with 'value1' are the different keys and values after the comparison

# Example:

const objA = {
  name: 'Wilson',
  age: 33,
  score: [2,5,7,8]
};

const objB = {
  name: 'Barbara',
  age: 31,
  score: [2,5,7,8]
};

const result = deepDiff(objA, objB);

The output would be:

result = {
  one: [ { name: 'Wilson' }, { age: 33 } ],
  two: [ { name: 'Barbara' }, { age: 31 } ]
}; // name and age are different

NOTE You can also run node tests/debug to try it out first.

(React part, this was the original intent)

In order to improve a react component performance the method that's often used is 'shouldComponentUpdate', but like the internet says it's often dangerous and if used poorly it can break your component.

What this package provides is a way of, along with react's componentWillReceiveProps, know which are the props that are changing with every render of your component.

If there's some properties that you don't need to compare or they don't change at all (which you can check with this package) you can then leverage on shouldComponentUpdate to avoid unnecessary re-renders.

Use componentWillReceiveProps with it

componentWillReceiveProps(nextProps) {
  const result = deepDiff(this.props, nextProps);

  // Log the result and identify the changes
  console.log(result);
}

Identify the bottlenecks and leverage shouldComponentUpdate

shouldComponentUpdate(nextState, nextProps) {
  // assume 'name' is a prop that changes all the time but it doesn't
  // concern your component (it shouldn't re-render if that prop changes)
  if (this.props.name !== nextProps.name) {
    return false;
  }

  return true;
}
  • Found an issue? https://github.com/jpggvilaca/deep-diff-object/issues
  • Want to contribute? Make a PR and I'll check it out!