Filter vs For (v2)

Revision 2 of this benchmark created on


Description

is a for loop faster than a for

Setup

var arr = [...Array(1e6)].map((_, i) => ({ foo: i}));

function cb(o) {
	return !!o.foo === 500;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Filter
var result = arr.filter(cb);
ready
for
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]);
		}
	}
	
	return result;
}

var result = filter(arr, cb);
ready

Revisions

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