Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array in array search 2
(version: 0)
Comparing performance of:
reduce + find vs save on find
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
reduce + find
const asset = { "id": "d6341699-44d9-40d4-96a7-a9e6399790f4", "equipmentFamily": "19XRV", "picControllerVersion": "PIC3", } const telemetryResponse = { "findTelemetry": { "data": [{ "id": "-", "assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40", "timestamp": 1678968480000, "brickClass": "Alarm", "measureName": "al122", "measureValue": true, "createdAt": 1678982953477 }, { "id": "-", "assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40", "timestamp": 1678970700000, "brickClass": "Alarm", "measureName": "al160", "measureValue": true, "createdAt": 1678985127307 } ], "limit": 1000 } } const alarmMetadataResponses = [{ getAlarmMetaData: { data: [{ alarmCode: "239", chillerModel: "19XR", controllerType: "PIC2", type: "Alarm", level: 1, category: "PROTECTIVE LIMIT", resetType: "Manual", }, { alarmCode: "243", chillerModel: "19XR", controllerType: "PIC2", type: "Alarm", level: 3, category: "POTENTIAL FREEZE- UP", resetType: "Manual", }, ], }, }, { getAlarmMetaData: { data: [{ alarmCode: "253", chillerModel: "23XRV", controllerType: "PIC3", type: "Alarm", level: 1, category: "PROTECTIVE LIMIT", resetType: "Manual", }, ], }, }, ]; const getMappedAlarms = () => { const alarms = telemetryResponse?.findTelemetry?.data ?? []; const assetAlarms = alarms.filter((alarm) => asset.id === alarm?.assetId && Boolean(alarm)); const alarmMetadataArray = alarmMetadataResponses .reduce((acc, response) => { return [ ...acc, ...(response?.getAlarmMetaData?.data ?? []).filter((metadata) => Boolean(metadata)) ] }, []); return assetAlarms.map((alarm) => { const telemetryAlarmCode = alarm.measureName.replace(/\D/g, ""); const isMetadataMatches = (alarmMetadata) => { const { chillerModel, controllerType, alarmCode } = alarmMetadata ?? {}; if (!chillerModel && !controllerType && !alarmCode) { return false; } return ( asset.equipmentFamily === chillerModel && asset.picControllerVersion === controllerType && telemetryAlarmCode === alarmCode ); }; const metadata = alarmMetadataArray.find((data) => isMetadataMatches(data)); return getTransformedAlarm(alarm, metadata); }) } getMappedAlarms()
save on find
const asset = { "id": "d6341699-44d9-40d4-96a7-a9e6399790f4", "equipmentFamily": "19XRV", "picControllerVersion": "PIC3", } const telemetryResponse = { "findTelemetry": { "data": [{ "id": "-", "assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40", "timestamp": 1678968480000, "brickClass": "Alarm", "measureName": "al122", "measureValue": true, "createdAt": 1678982953477 }, { "id": "-", "assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40", "timestamp": 1678970700000, "brickClass": "Alarm", "measureName": "al160", "measureValue": true, "createdAt": 1678985127307 } ], "limit": 1000 } } const alarmMetadataResponses = [{ getAlarmMetaData: { data: [{ alarmCode: "239", chillerModel: "19XR", controllerType: "PIC2", type: "Alarm", level: 1, category: "PROTECTIVE LIMIT", resetType: "Manual", }, { alarmCode: "243", chillerModel: "19XR", controllerType: "PIC2", type: "Alarm", level: 3, category: "POTENTIAL FREEZE- UP", resetType: "Manual", }, ], }, }, { getAlarmMetaData: { data: [{ alarmCode: "253", chillerModel: "23XRV", controllerType: "PIC3", type: "Alarm", level: 1, category: "PROTECTIVE LIMIT", resetType: "Manual", }, ], }, }, ]; const getMappedAlarms = () => { const alarms = telemetryResponse?.findTelemetry?.data ?? []; const assetAlarms = alarms.filter((alarm) => asset.id === alarm?.assetId && Boolean(alarm)); return assetAlarms.map((alarm) => { const telemetryAlarmCode = alarm.measureName.replace(/\D/g, ""); const isMetadataMatches = (alarmMetadata) => { const { chillerModel, controllerType, alarmCode } = alarmMetadata ?? {}; if (!chillerModel && !controllerType && !alarmCode) { return false; } return ( asset.equipmentFamily === chillerModel && asset.picControllerVersion === controllerType && telemetryAlarmCode === alarmCode ); }; const metadata = null; alarmMetadataResponses.find((response) => { const metadata = response?.getAlarmMetaData?.data?.find((data) => isMetadataMatches(data)); return metadata; }); return getTransformedAlarm(alarm, metadata); }) } getMappedAlarms()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce + find
save on find
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
This is a complex data processing code snippet, and I'll do my best to break it down and provide an answer. **Code Overview** The code appears to be part of a larger application that processes telemetry data from various sources (e.g., sensors, equipment) and maps the data to alarm metadata. The `getMappedAlarms` function is responsible for filtering and transforming the telemetry data based on certain conditions. **Key Functions and Variables** 1. `findTelemetry`: A function that returns a filtered array of telemetry data. 2. `filter((alarm) => asset.id === alarm?.assetId && Boolean(alarm))`: A filter function that selects alarms with an asset ID matching the current `asset.id`. 3. `map((alarm) => { ... })`: A map function that transforms each alarm object. 4. `isMetadataMatches`: A function that checks if a given telemetry alarm matches a specific metadata pattern. 5. `getTransformedAlarm(alarm, metadata)` : A function that takes an alarm object and metadata (if available) to transform the alarm data. **Key Data Structures** 1. `telemetryResponse`: An object containing the filtered telemetry data. 2. `asset.id`: The ID of a specific asset. 3. `alarm.measureName`: The name of a telemetry alarm. 4. `alarmMetadataResponses`: An array of objects containing alarm metadata responses. 5. `getAlarmMetaData`: A property within each `alarmMetadataResponse` object that contains the alarm metadata data. **Questions** Based on the provided code snippet, I have two questions to clarify: 1. What is the purpose of the `findTelemetry` function? How does it filter and process the telemetry data? 2. Can you provide more context or information about the `getTransformedAlarm` function? How does it transform the alarm data based on the metadata pattern? Please let me know if I can help with anything else!
Related benchmarks:
findIndex vs for-of
JS find vs indexOf 3
JS find vs indexOf 4
JS typed strict find vs indexOf
array.find and boolean cast or array.some
Comments
Confirm delete:
Do you really want to delete benchmark?