reduce + spread vs filter + map

Benchmark created on


Setup

const data = Array.from({length: 1000}, () => {
    const latNull = Math.random() < 0.1
    const lonNull = Math.random() < 0.1
    return {
        lat: latNull ? null : Math.random(),
        lon: lonNull ? null : Math.random(),
        other: new Date().toString()
    }
});

Test runner

Ready to run.

Testing in
TestOps/sec
reduce + spread operator
data.reduce((acc, item) => {
    if (!item.lat || !item.lon) return acc;

    return [...acc, [item.lon, item.lat]]
}, [])
ready
filter + map
data.filter(item => item.lat && item.lon)
  .map(item => [item.lon, item.lat])
ready

Revisions

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