I want to merge two Array's in JavaScript. Unfortunately, comparisons of the particular data I am using is expensive. What is the best algorithm to merge my to lists with the least amount of comparisons?
EDIT: I should note that the two Array's are sorted and I would like the merged content to be sorted and have only unique values.
EDIT: By request I will give you my current code, but it really doesn't help..
// Merges arr1 and arr2 (placing the result in arr1)
merge = function(arr1,arr2) {
if(!arr1.length) {
Array.prototype.push.apply(arr1,arr2);
return;
}
var j, lj;
for(var s, i=0, j=0, li=arr1.length, lj=arr2.length; i<li && j<lj;) {
s = compare(arr1[i], arr2[j]);
if(s<0) ++i;
else if(s==0) ++i, ++j;
else arr1.splice(i,0, arr2[j++]);
}
if(j<lj) Array.prototype.push.apply(arr1, arr2.slice(j));
};