Filter vs For

Benchmark created on


Description

is a for loop faster than a for

Test runner

Ready to run.

Testing in
TestOps/sec
Filter
var arr = [...Array(1e6)].map((_, i) => ({ foo: i}));

var result = arr.filter((o) => o.foo === 500);
ready
for
var arr = [...Array(1e6)].map((_, i) => ({ foo: i}));

function filter(array, callback) {
	var result = [];
	var l = array.length;

	for (var i = 0; i < l; i++) {
		if (callback(arr[i])) {
			result.push(arr[i]);
		}
	}
}

function cb(o) {
	return !!o.foo === 500;
}
filter(arr, cb);
ready

Revisions

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