Самый быстрый способ объединить два массива (v4)

Revision 4 of this benchmark created on


Setup

const arr1 = Array.from({length:1000}, (_,i) => i + 100000);
const arr2 = Array.from({length:1000}, (_,i) => i);

Test runner

Ready to run.

Testing in
TestOps/sec
fixed length + for + for
const l1 = arr1.length;
const l2 = arr2.length;
const arr3 = new Array(l1 + l2);

for (let i = 0; i < l1; i++) {
	arr3[i] = arr1[i];
}

for (let i = 0, j = l1; i < l2; i++, j++) {
	arr3[j] = arr2[i];
}
ready
slice + for
const count = arr1.length + arr2.length;
const arr3 = arr1.slice();
arr3.length = arr1.length + arr2.length;

for (let i = arr1.length, j = 0; i < count; i++, j++) {
	arr3[i] = arr2[j];
}
ready
concat
const arr3 = arr1.concat(arr2)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.