Array.prototype.every vs. native for-loop with early return

Benchmark created on


Setup

const size = 100000;

const randomItems = []
const maxItems = []
const minItems = []
function buildArray() {
	for (let i = 0; i < size; i++) {
		randomItems.push({
			someProp: Math.random(),
		});
		maxItems.push({
			someProp: 1,
		});
		minItems.push({
			someProp: 0,
		});
	}
}
buildArray();


function testEvery(items) {
	return items.every(item => item.someProp >= 0.5);
}

function testNative(items) {
	for (let i = 0; i < items.length; i++) {
		if (!items[i].someProp >= 0.5) return false;
	}
	return true;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Array.prototype.every random items
testEvery(randomItems);
ready
Native for-loop random items
testNative(randomItems);
ready
Array.prototype.every max items
testEvery(maxItems);
ready
Native for-loop max items
testNative(maxItems);
ready
Array.prototype.every min items
testEvery(minItems);
ready
Native for-loop min items
testNative(minItems);
ready

Revisions

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