Skip to content

Instantly share code, notes, and snippets.

@Kikobeats
Created January 2, 2017 22:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kikobeats/ff5dea8601462c3380277bfa4ba93a86 to your computer and use it in GitHub Desktop.
Save Kikobeats/ff5dea8601462c3380277bfa4ba93a86 to your computer and use it in GitHub Desktop.
hyperdiff
'use strict'
const _simpleArrayDiff = require('simple-array-diff')
const { map, range, random } = require('lodash')
const suite = require('fastbench')
const _hyperDiff = require('.')
const _range = range(0, 1000)
function generateRange (_range) {
return map(_range, () => { return { id: random(0, 100) } })
}
const array1 = generateRange(_range)
const array2 = generateRange(_range)
function bench (fn) {
fn(array1, array2, 'id')
}
const run = suite([
function simpleArrayDiff (done) {
bench(_simpleArrayDiff)
done()
},
function hyperDiff (done) {
bench(_hyperDiff)
done()
}
], 1000)
// run them two times
run(run)
'use strict'
const { difference, reduce, findIndex, pullAt } = require('lodash')
const isPresent = (itemIndex) => itemIndex !== -1
function hyperdiff (orig, dist, fn) {
const results = reduce(dist, function (acc, item) {
const itemIndex = findIndex(orig, item, fn)
if (isPresent(itemIndex)) acc.common.push(item)
else acc.added.push(item)
pullAt(orig, itemIndex)
return acc
}, {
added: [],
common: []
})
results.removed = difference(orig, results.common)
return results
}
module.exports = hyperdiff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment