Last active
March 21, 2018 06:28
-
-
Save SeeThruHead/6809967e50b2e64a4046d269017935b8 to your computer and use it in GitHub Desktop.
what's faster for uniqueness in an array
This file contains 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
require('babel-polyfill'); | |
var uuid = require('uuid'); | |
var faker = require('faker'); | |
var Benchmark = require('benchmark'); | |
var suite = new Benchmark.Suite; | |
var chunkify = require('chunkify'); | |
const createFakeArray = range => { | |
const arr = []; | |
for (let i = 0; i < range; i ++) { | |
arr.push({ | |
fName: faker.name.firstName(), | |
lName: faker.name.lastName(), | |
password: faker.internet.password(), | |
email: faker.internet.email(), | |
id: uuid() | |
}); | |
} | |
return arr; | |
}; | |
const addDupes = input => input.reduce((acc, curr) => { | |
acc.push(curr); | |
if (Math.random() < .2) acc.push(curr); | |
return acc; | |
}, []); | |
const array = addDupes(createFakeArray(1.5e3)); | |
console.log('Array length after adding duplicates'); | |
console.log(array.length); | |
// push method uses UUID for duplication | |
suite.add('push with object lookup uuid comparison', function() { | |
const map = {}; | |
const result = array.reduce((acc, curr) => { | |
if (map[curr.id]) { | |
return acc; | |
} | |
map[curr.id] = true; | |
acc.push(curr); | |
return acc; | |
}, []); | |
}) | |
// reference equality via indexOf | |
.add('push indexOf', function() { | |
const result = array.reduce((acc, curr) => { | |
if (acc.indexOf(curr) > -1) { | |
return acc; | |
} | |
acc.push(curr); | |
return acc; | |
}, []); | |
}) | |
// reference equality via includes | |
.add('push using includes', function() { | |
const result = array.reduce((acc, curr) => { | |
if (acc.includes(curr)) { | |
return acc; | |
} | |
acc.push(curr); | |
return acc; | |
}, []); | |
}) | |
// set method uses reference to detect dupes | |
.add('create a set', function() { | |
const result = [...array.reduce((acc, curr) => acc.add(curr), new Set)]; | |
}) | |
// set method with chunkify | |
.add('use set with chunkify', { | |
defer: true, | |
fn: function(deferred) { | |
chunkify.reduce(array, (acc, curr) => acc.add(curr), { chunk: 2000 }, new Set).then(result => { | |
[...result]; | |
deferred.resolve(); | |
}); | |
} | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); // set is faster [node used: 6.9.2] | |
}) | |
.run({ 'async': true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment