cyber,
сравнение количества операций для преобразования и длины нового массива ...
function rand()
{ var l = Math.round(Math.random() * 100), a = [];
for (var i=0; i<l; i++) {a[i]=Math.round(Math.random() * 100)}
return a
}
function compare_indexOf(fArr,arr) {
var search = arr.slice(),
insert = [],
remove = [],
replace = [];
for(var i = 0, index; i < fArr.length; i++) {
index = search.indexOf(fArr[i]);
if(!~index) {
remove.push(i);
continue;
}
i != index && replace.push({'old': i, 'new': index});
delete search[index];
};
search.forEach(function (elem, i) {
if(elem)
insert.push(i);
});
return {
'remove': remove,
'insert': insert,
'replace': replace
}
}
function compare(f, d) {
var e = {
relocation: [],
insert: [],
length: d.length
}, c = d + ", " + f ,
b = f.slice();
if (c = c.match(/([^,]\S*[^,])(?=,[\s\S]*\s[\s\S]*\1)/g)) {
c.reverse();
for (var a = 0; a < c.length; a++) {
var g = ("" + b).slice(0, ("" + b).lastIndexOf(c[a]) + 1).split(",").length - 1,
h = c[a].split(",").length,
k = b.splice(g, h);
e.relocation.push([g, h]);
b = k.concat(b)
}
}
for (a = 0; a < d.length; a++) d[a] != b[a] && (b.splice(a, 0, d[a]), e.insert.push([a, d[a]]));
return e
};
var oldArray = rand();
var newArray = rand();
var time_indexOf = new Date();
var changes = compare_indexOf(oldArray, newArray);
time_indexOf = (new Date()).getTime() - time_indexOf.getTime()
var len_indexOf = changes.remove.length + changes.insert.length + changes.replace.length;
var time = new Date();
var changes = compare(oldArray, newArray);
time = (new Date()).getTime() - time.getTime()
var len = changes.relocation.length + changes.insert.length + 1;
alert('len_indexOf : ' + len_indexOf + ' time: ' + time_indexOf + '\nlen: ' + len + ' time: ' + time + '\nlength : ' + newArray.length);