Test chaining array func

Benchmark created on


Setup

const arr = [];
const arr2 = [];
const arr3 = [];
class BaseObject {
	selected = false;
	orderProduct = {
		code: -1,
		ean: "12345",
		label: "12345",
		line: 1,
		quantity: 1,
		state: "12345",
		transaction: "12345",
		availability: "12345",
		creation: "12345",
		update: "12345",
		origin: "12345"
	}

	constructor(id, selected) {
		this.selected = selected;
		this.orderProduct.code = id;
	}
}

for (let i = 0; i < 100; i++) {
	arr.push(new BaseObject(i, i % 2 === 0));
}

for (let i = 0; i < 100; i++) {
	arr2.push(new BaseObject(i, i % 3 === 0));
}

Test runner

Ready to run.

Testing in
TestOps/sec
ForEach
arr2.forEach(a => {
	if (a.selected && arr.some(x => x.orderProduct.code === a.orderProduct.code)) {
		arr3.push(a);
	}
});
ready
Chaining
arr2
.filter(a => a.selected)
.filter(a => arr.some(x => x.orderProduct.code === a.orderProduct.code))
.forEach(a => arr3.push(a));
	
ready
Chaining 2
arr2
.filter(a => a.selected && arr.some(x => x.orderProduct.code === a.orderProduct.code))
.forEach(a => arr3.push(a));
ready

Revisions

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