forEach vs filter and find

Benchmark created on


Setup

 const offences = [
    { mostSerious: false },
    { mostSerious: false },
    { mostSerious: false },
    { mostSerious: true },
    { mostSerious: false },
    { mostSerious: false },
 ]

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
let indexOffence
let additionalOffences = []

offences.forEach(offence => {
	if (offence.mostSerious) {
		indexOffence = offence
	} else {
		additionalOffences.push(offence)
	}
})
      
return {
	indexOffence,
	additionalOffences
}
ready
filter and find
return {
  additionalOffences: offences.filter(offence => !offence.mostSerious),
  indexOffence: offences.find(offence => offence.mostSerious),
}
ready

Revisions

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