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

Revision 2 of this benchmark created on


Setup

const arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const arr2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Test runner

Ready to run.

Testing in
TestOps/sec
modify arr1: push with spread
arr1.push(...arr2)
ready
new arr3: concat
const arr3 = arr1.concat(arr2);
ready
modify arr1: forEach by arr2 + index
arr2.forEach(i => {
	arr1[arr1.length] = i;
})
ready
new arr3: spread arr1 and arr2
const arr3 = [...arr1, ...arr2];
ready
new arr3: new Array(fixedLength) + for loop
const count = arr1.length + arr2.length;
const arr3 = new Array(count);

for (let i = 0; i < count; i++) {
	arr3[i] = i < arr1.length ? arr1[i] : arr2[i-arr1.length];
}
ready
@CrazyElf suggestion
const count = arr1.length + arr2.length;
const arr3 = new Array(count);

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

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

Revisions

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