What is the fastest way to copy an array?
I decided to make a game, but I feel that Array.filter is too slow, so I created a method:
Array.prototype.removeIf = function(condition: Function): any[] {
var l: any[] = Array.from(this);
var __begidx = 0;
for (var i: number = l.length; --i; ) {
/** i; */
if (condition(l[i])) {
l[i] = l[l.length - 1];
__begidx += 1;
}
}
l.length -= __begidx;
return l;
}
if without Array.from, this method takes only 130 - 295ms to remove 99999999 elements, because it just put element to remove at end of list, and slice out. Even filter takes 670 - 1330 ms to remove.
but when I test Array.from:
var timeit = new TimeIt(); TimeIt.set();
var range = Array.from(arr) // 'arr' is a Array contains 99999999 elements
timeit.print(); // 1320 - 1634ms
Why is Array.from so slow? It's 10 times slower than 'removeIf', and slower than Array.filter. Are there any faster way to copy list?