You'll get the best performances by using most appropriate data structure. In a JIT/interpreted language like js, the gains of using a native functionality are tremendous.
Here it's a Set you should use in the first place : this way you don't even have to do anything to remove dups, they will just be ignored when added.
I just did a simple test, and performances are around six to ten times (!!) faster with a Set.
http://jsbin.com/jofofeyixaco/1/edit?js,console
result example :
"overhead is : 0.015700000221841037"
"Built unic numbers with a lookup object in : 6.237600000167731"
"Built unic numbers with a Set in : 0.7921500000520609"
Here are the curves for n = 0 to 50.000 for both algorithm.
We see that indeed the hashmap behaves quite like O(1), but with a higher spread when n raises.
Set is almost perfectly linear.
drawing jsbin (be patient ! ) : http://jsbin.com/jofofeyixaco/2/
Code :
// noprotect
// build a test set
var numbers = [];
var cnt = 10000;
for (var i=0; i<cnt; i++ ) numbers.push(Math.floor(Math.random*1000));
// build unic values using lookup object
function buildWithObject() {
var existing= {};
var unicNumbers = [];
for (var i=0; i<cnt; i++) {
var num = numbers[i];
if (!existing[num]) {
unicNumbers.push(num);
existing[num]=true;
}
}
}
// build unic values using a Set
function buildWithSet() {
var unicNumbersSet = new Set();
for (var i=0; i<cnt; i++) {
var num = numbers[i];
unicNumbersSet.add(num);
}
}
function iterate() {
for (var i=0; i<cnt; i++) {
var num = numbers[i];
}
}
// warming up functions
for (var i=0; i<30; i++) { buildWithObject(); buildWithSet() ; iterate(); }
// -------- Measures --------------------
var measureRepeat = 20;
var m;
var s,e;
// ----------------------------
m=measureRepeat;
s=window.performance.now();
while (m--) iterate();
e=window.performance.now();
console.log('overhead is : ' + (e-s)/measureRepeat);
// ----------------------------
m=measureRepeat;
s=window.performance.now();
while (m--) buildWithObject();
e=window.performance.now();
console.log('Built unic numbers with a lookup object in : ' + (e-s)/measureRepeat);
// ----------------------------
m=measureRepeat;
s=window.performance.now();
while (m--) buildWithSet();
e=window.performance.now();
console.log('Built unic numbers with a Set in : ' + (e-s)/measureRepeat);
(don't forget, Set is EcmaScript 6, so use, in the js tag, type="application/javascript;version=1.7"
If you're concerned about compatibility : http://kangax.github.io/compat-table/es6/#Set
All 'modern' platforms ok : Ch, FF, IE11, OS8
All others not ok. )
existObjas a hash map - with nearO(1)performance for assigning and accessing.(k)size of the key (e.g. length of the property string),(v)size of the stored values, and(n)number of items in the map - just with a very good complexity. (disclaimer:vis constant for booleans like here, or object pointers;kcan be reduced by a lazy hashing function that doesn't visit all bytes; andncould be limited if the key space is finite)