In IE8 I've experienced absurdly slow performance when comparing a bunch of floats. In that case asynchronous handling is the only option you have to not get the "script takes too long" message.
IE8 does not have Array.filter function so your code would not work in IE8 anyway.
//create an array with 10.000 floats
var arr=[];
for(i=0;i<10000;i++){
arr.push(Math.random());
}
//adding one double float to make sure it will be removed
arr.push(arr[5]);
console.log("array created:",arr.length);
var now=new Date();
function getUniqueDone(arr){
console.log("done asynch",(new Date().getTime())-now);
console.log("length of array:"+arr.length);//usually is 10.000
}
// works only on arrays containing numbers or strings
function getUnique(arr,callback){
var i,j,len,counter,
chuncksOf=1000;//In IE8 I get script warnings at 150 already,
// FF and Chrome could handle 1000
// depending on what your array contains you can test a bit and
// set chuncksOf accordingly. current value does nnot work in IE8
// you have to set it to 100 and wait 20 seconds for output
if(arr.sort){
// on very large arrays this still might have a problem
console.log("sorting");
arr.sort();
console.log("sort done");
i=0;
}else{
i=arr.i;
arr=arr.arr;
}
len=arr.length;
counter=0;
while(i<len&&counter<chuncksOf){
for(j=i+1;j<arr.length;j++){
if(arr[i]===arr[j]){
arr.splice(j,1);
j--;
}
}
i++;
counter++;
}
if(i>=len){
callback(arr);
}else{
var pass={
arr:arr,
i:i
}
console.log("i is now:",i);
setTimeout(function(){
getUnique(pass,callback);
},0);
}
}
console.log("getting unique");
getUnique(arr,getUniqueDone);