Created
May 19, 2014 03:48
-
-
Save nevill/551c4855e16b0cf108ab to your computer and use it in GitHub Desktop.
Simpe test on how to strip out duplications from array in Javascript
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
var _ = require('underscore'); | |
var ObjectID = require('mongodb').ObjectID; | |
var duplicatedIds = []; | |
var lengthOfDuplicatedIds = 10; | |
for (var i = 0; i < lengthOfDuplicatedIds; i++) { | |
duplicatedIds.push(new ObjectID()); | |
} | |
var ArrToTest = []; | |
// change this value to increate the size of testing set | |
var lengthOfArrayToTest = 8000; | |
for (var j = 0; j < lengthOfArrayToTest; j++) { | |
ArrToTest.push(new ObjectID()); | |
} | |
ArrToTest = _.shuffle(duplicatedIds.concat(ArrToTest).concat(duplicatedIds)); | |
function runTest(func) { | |
var start = new Date(); | |
func.call(); | |
var end = new Date(); | |
console.log('Using %s: %d milliseconds', func.name, (end.valueOf() - start.valueOf())); | |
} | |
function indexOf() { | |
return _.uniq(ArrToTest); | |
} | |
function hash() { | |
return Object.keys( | |
ArrToTest.reduce(function(r, value) { | |
r[value.toString()] = 1; | |
return r; | |
}, {}) | |
); | |
} | |
runTest(indexOf); | |
runTest(hash); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment