Search code examples
muledataweavemule4

Is there a performance difference between using flatten(...map()...) vs flatMap()?


I'm trying to understand if using flatMap() is exactly the same that doing a map() inside flatten() with regards to performance and resource usage.

For example in my tests comparing

%dw 2.0
output application/json
---
flatten(payload.prefixes map {
      "ip_prefix": $.ip_prefix,
      "region": $.region++"-beta",
      "service": "NewCorp",
      "network_border_group": $.network_border_group
} 
)

and

%dw 2.0
output application/json
---
payload.prefixes flatMap {
      "ip_prefix": $.ip_prefix,
      "region": $.region++"-beta",
      "service": "NewCorp",
      "network_border_group": $.network_border_group
} 

I didn't notice a performance difference.


Solution

  • It should be the same no mayor performance implications between one and the other. I usually prefer using flatMap as it reads better, but that is a personal opinion.