1.0.1 • Public • Published


A small utility to do "partial" deep comparisons of two objects.


React's pure render mixin wasn't cutting it for us, but I didn't want to do a deep inspection of everything on the props every time either. I hand wrote some shouldComponentUpdate functions and wanted to generalize. This gave me something that I could configure at use-time to allow the kind of comparison I had in mind.


npm install schema-compare

var schemaCompare = require('schema-compare');
var schema = {
    foo: [],
    bar: [{ baz: null }]
    quux: null

// ...
schemaCompare(schema, obj1, obj2);

The way it works is as follows:

  • If given a simple value (not an object or an Array instance), it will perform a strict comparison of the two arguments (schemaCompare(1, 2, 3) === false, schemaCompare(null, 'foo', 'foo') === true)
  • If given an array, it will compare the length and then contents of the arguments (schemaCompare([], [1], [1, 1]) === false, schemaCompare([], [1], [2]) === false, schemaCompare([], [1, 2, 3], [1, 2, 3]) === true)
  • If given an object, it will verify the presence of the specified keys on the arguments and compare their values. Unspecified keys are not compared. (schemaCompare({foo:null}, {foo: 1}, {foo: 2}) === false, schemaCompare({foo:null}, {foo: 1, bar: 2}, {foo: 1, bar: 3}) === true)
  • The first index of an array in the schema and the values of the keys in the schema will recursively call schemaCompare to compare the associated data from the arguments (schemaCompare([ {foo:null} ], [{foo:1}, {foo:2}], [{foo:1}, {foo:2}]) === true, schemaCompare([ {foo:null} ], [{foo:1}, {foo:2}], [{foo:1}, {foo:3}]) === false)

When you need to specify a key in an object to be compared, but the values are simple values, you can specify anything except an array or an object. I make a point of using null in the examples since typeof null === 'object' to illustrate that this case is covered.

Multiple arguments to an array in the schema definition is erroneous; only the first index will be checked. No error checking is done on the structure of the data you pass: if you pass data that doesn't conform to your schema, you will likely get not-very-helpful error messages sourced in this module.


npm test for unit tests npm run cov for unit tests with coverage report (currently 100%)

The tests are autogenerated from permutations of many different kinds of values and augmented by some targeted error and shortcut cases.



Package Sidebar


npm i schema-compare

Weekly Downloads






Last publish


  • myndzi