forEach和for性能对比

Benchmark created on


Setup

const length = 100000;

Test runner

Ready to run.

Testing in
TestOps/sec
forEach测试
const arr = Array.from(
	{
		length,
	},
	(v, i) => i,
);
console.time('forEach');
arr.forEach(element => {
	element * 2;
});
console.timeEnd('forEach');
ready
for测试
const arr = Array.from({ length }, (v, i) => i);
console.time('for');
for (let i = 0; i < arr.length; i++) {
	arr[i] * 2;
}
console.timeEnd('for');
ready

Revisions

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