Which is better performance?
Option 1
var sections: [SubMenu]
var selectedItem: BeverageItem? {
var item: BeverageItem?
for section in sections {
for beverage in section.beverages {
if beverage.isSelected {
item = beverage
break
}
}
}
return item
}
Option 2
var selectedItem: BeverageItem? {
var item: BeverageItem?
if let subMenuIndex = sections.index(where: { (subMenu) -> Bool in
for beverage in subMenu.beverages {
if beverage.isSelected {
return true
}
}
return false
}) {
let subMenu = sections[subMenuIndex]
if let beverageIndex = subMenu.beverages.index(where: { (beverage) -> Bool in
return beverage.isSelected
}) {
item = subMenu.beverages[beverageIndex]
}
}
return item
}
If option 1 then where would you use option 2 and why?