Array.filter vs Generator

Benchmark created on


Preparation HTML

	

Setup

const daily = [];
const end = new Date('2024-03-13');
for (const current = new Date('2000-12-31'); current < end; current.setUTCDate(current.getUTCDate() + 1)) {
    const currentDateStr = current.toISOString().slice(0, 10);
    daily.push({
      period: currentDateStr,
      start: currentDateStr,
      end: currentDateStr,
    });
}

Test runner

Ready to run.

Testing in
TestOps/sec
Array.filter
function filterDates(start, end) {
  return daily.filter(d => d.start >= start && d.start <= end);
}

for (const date of filterDates('2005-04-01', '2023-01-01')) {
  console.log(date);
}
ready
Generator
function* filterDates(start, end) {
  for (const d of daily) {
    if (d.start >= start && d.start <= end) {
      yield d;
    }
  }
}

for (const date of filterDates('2005-04-01', '2023-01-01')) {
  console.log(date);
}
ready

Revisions

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