forEach, for..of, reduce and filter+map (v4)

Revision 4 of this benchmark created on


Description

compare all of these approaches

Preparation HTML

<script>
let sensors = Array(50).fill().map(e =>  Math.random() < 0.5 ? ({ ID: Math.random(), loc: Math.random() }) : Math.random() < 0.5 ? ({ ID: Math.random()}): ({}));



</script>

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
            const sensorMapping = [];
            const unmappedSensors = [];

            sensors?.forEach((sensor) => {
                if (sensor.loc && sensor.ID) {
                    sensorMapping.push({
                        serialNumber: sensor.ID,
                        location: sensor.loc,
                    });
                } else if (sensor.ID) {
                    unmappedSensors.push(sensor.ID);
                }
            });
ready
filter+map
            const sensorMapping = sensors?.filter(sensor =>
                sensor.loc && sensor.ID
            ).map(sensor => ({
                serialNumber: sensor.ID,
                location: sensor.loc,
            })) || [];
            const unmappedSensors = sensors?.filter(sensor => !sensor.loc && sensor.ID).map(sensor => sensor.ID) || [];
           
            
ready
for...of
            const sensorMapping = [];
            const unmappedSensors = [];

            if (sensors) {
                for (const sensor of sensors) {
                    if (sensor.loc && sensor.ID) {
                        sensorMapping.push({
                            serialNumber: sensor.ID,
                            location: sensor.loc,
                        });
                    } else if (sensor.ID) {
                        unmappedSensors.push(sensor.ID);
                    }
                }
            }
ready
reduce
            const { sensorMapping, unmappedSensors } = (sensors || []).reduce(({ sensorMapping, unmappedSensors }, sensor) => { 
                if (sensor.loc && sensor.ID) {
                    sensorMapping.push({
                        serialNumber: sensor.ID,
                        location: sensor.loc,
                    });
                } else if (sensor.ID) {
                    unmappedSensors.push(sensor.ID);
                }
                return { sensorMapping, unmappedSensors };
            }, {
                sensorMapping: [],
                unmappedSensors: [],
            })
ready
reduce without destructuring
            const { sensorMapping, unmappedSensors } = (sensors || []).reduce((acc, sensor) => { 
                if (sensor.loc && sensor.ID) {
                    acc.sensorMapping.push({
                        serialNumber: sensor.ID,
                        location: sensor.loc,
                    });
                } else if (sensor.ID) {
                    acc.unmappedSensors.push(sensor.ID);
                }
                return acc;
            }, {
                sensorMapping: [],
                unmappedSensors: [],
            })
ready

Revisions

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