jQuery.each vs Array.forEach

Benchmark created on


Preparation HTML

<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

Setup

var foo = ['aa', 'bbb', 'ccc', 'd', 'e', 'f', 'ggg', 'i'];
var bar = {'aa': '1', 'bbb': '2', 'ccc': 'c', 'd': 'true', 'e': 'false', 'f': 'f', 'ggg': 'g', 'i': 'i'};

var state; 
function something(a, b) {
	state = a.length + b.length;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Array.forEach
foo.forEach(function (val) {
	something(val, val);
});
ready
Array for-loop
for (var i = 0; i < foo.length; i++) {
	var val = foo[i];
	something(val, val);
}
ready
Object.keys().forEach()
Object.keys(bar).forEach(function (key) {
	const val = bar[key];
	something(key, val);
});
ready
jQuery.each(Object)
$.each(bar, function (key, val) {
	something(key, val);
});
ready
Object for-in
for (var key in foo) {
	var val = foo[key];
	something(val, val);
}
ready

Revisions

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