Created
January 2, 2017 22:45
-
-
Save Kikobeats/ff5dea8601462c3380277bfa4ba93a86 to your computer and use it in GitHub Desktop.
hyperdiff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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