GithubHelp home page GithubHelp logo

joelwass / js-deep-equals Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 2.0 272 KB

testing of array deep equals 66% faster than JSON.stringify, accounts for nested arrays and objects

License: MIT License

JavaScript 100.00%
array deep equals deepequal nestedarrays nestedarray unsortedarray unsortedarrayequals objects nested

js-deep-equals's Introduction

js-deep-equals

Build Status Coverage Status

testing of array and object deep equality (unsorted and sorted), accounting for nested arrays and nested objects

faster than JSON.stringify(x) === JSON.stringify(y) and Lodash's isEqual (benchmark code here)

screenshot

unsorted arrays are compared by creating a Merkle Tree out of the input and comparing the top level hashes. hashing is done using murmur v3.

usage

npm install js-deep-equals

const { compare, compareUnsorted } = require('js-deep-equals')
const arr1 = [
  1,
  2,
  3,
  'test',
  'test2',
  'test3',
  {
    a: 12,
    b: 13,
    c: 14,
    d: [ 71, 72, 73, { 'sonested': true } ]
  }
]

// order and contents are the same as arr1
let arr2 = [
  1,
  2,
  3,
  'test',
  'test2',
  'test3',
  {
    b: 13,
    a: 12, // different order of objects is ok
    c: 14,
    d: [ 71, 72, 73, { 'sonested': true } ]
  }
]

// order of this array is different than arr1, but content is the same
let arr3 = [
  1,
  'test',
  2,
  3,
  'test2',
  {
    b: 13,
    a: 12,
    d: [ 71, 72, { 'sonested': true }, 73 ]
    c: 14,
  },
  'test3'
]

compare(arr1, arr2) // true
compare(arr1, arr3) // false
compareUnsorted(arr1, arr3) // true

License

MIT

js-deep-equals's People

Contributors

joelwass avatar stripedpajamas avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

js-deep-equals's Issues

Cannot convert undefined or null to object

Running into an issue when the compared objects have a null or undefined value, which is common in my use case comparing deep objects.

const result = compareUnsorted({ a: null }, { a: [3, 2, 1] });

Throws:

Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at createTree (unsorted.js:31)
    at createTree (unsorted.js:55)
    at createTree (unsorted.js:55)
    at createTree (unsorted.js:55)
    at createTree (unsorted.js:55)
    at createFinalHash (unsorted.js:65)
    at compareUnsorted (unsorted.js:71)

Looks like this line:

const isObject = typeof currentInput === 'object'
const isArray = Array.isArray(currentInput)
const keys = Object.keys(currentInput)

There should probably be a short-circuit somewhere?

unsorted compare function is very susceptible to collisions

consider the very small example:

const unsorted = require('./src/unsorted')

let a = [0, ['ICV']]
let b = [['hBV'], 0]

console.log(unsorted(a, b)) // prints true, should be false

Because ICV and hBV both hash to 983781196 in the algorithm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.