only-shallow
v1.2.0
Published
like `deeper` and `deepest`, but less strict, and with 90s flavor
Downloads
47,904
Maintainers
Readme
only-shallow
If deeper
and deepest
are
assert.deepEqual()
's strict East Coast siblings with engineering backgrounds,
only-shallow
is their laid-back California cousin. only-shallow
is a
library for structurally comparing JavaScript objects. It supports recursive /
cyclical data structures, is written to avoid try / catch / throw (for speed),
and has no dependencies. It's not particularly strict about matching types.
It's more of a duck squeezer.
It has some optimizations but stresses correctness over raw speed. Unlike
deepest
, it has no native dependencies, so you can use it, like, wherever.
If you install Ben Noordhuis's
buffertools into a project
using only-shallow
, it will use that to speed up comparison of Buffers.
The core algorithm is based on those used by Node's assertion library and the implementation of cycle detection in isEqual in Underscore.js.
I like to think the documentation is pretty OK.
only-shallow
has this name because I'm
old.
installation
npm install only-shallow
usage
var deepEqual = require('only-shallow')
if (!deepEqual(obj1, obj2)) console.log("yay! diversity!");
details
Copied from the source, here are the details of only-shallow
's algorithm:
- Use loose equality (
==
) only for value types (non-objects). This is the biggest difference betweenonly-shallow
anddeeper
/deepest
.only-shallow
cares more about shape and contents than type. This step will also catch functions, with the useful (default) property that only references to the same function are considered equal. 'Ware the halting problem! null
is an object – a singleton value object, in fact – so if either isnull
, return a == b. For the purposes ofonly-shallow
, loose testing of emptiness makes sense.- Since the only way to make it this far is for
a
orb
to be an object, ifa
orb
is not an object, they're clearly not the same. - It's much faster to compare dates by numeric value (
.getTime()
) than by lexical value. - Compare RegExps by their components, not the objects themselves.
- The parts of an arguments list most people care about are the arguments themselves, not the callee, which you shouldn't be looking at anyway.
- Objects are more complex:
- Return
true
ifa
andb
both have no properties. - Ensure that
a
andb
have the same number of own properties (which is whatObject.keys()
returns). - Ensure that cyclical references don't blow up the stack.
- Ensure that all the key names match (faster).
- Ensure that all of the associated values match, recursively (slower).
- Return
license
ISC. Go nuts.