Skip to content

Commit

Permalink
Minor changes in Optional Collection extensions. 🚀 (#1110)
Browse files Browse the repository at this point in the history
- Instead of checking for nil using guard statements we are using self and some basic conditions
- This optimises the performance significantly considering that collection size can be bigger and checking using guard can be expensive.

---------

Co-authored-by: Mayank <>
  • Loading branch information
Mayank-84 committed Jun 19, 2023
1 parent 9621b02 commit 6ed83b7
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions Sources/SwifterSwift/SwiftStdlib/OptionalExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,12 @@ public extension Optional {
public extension Optional where Wrapped: Collection {
/// SwifterSwift: Check if optional is nil or empty collection.
var isNilOrEmpty: Bool {
guard let collection = self else { return true }
return collection.isEmpty
return self?.isEmpty ?? true
}

/// SwifterSwift: Returns the collection only if it is not nil and not empty.
var nonEmpty: Wrapped? {
guard let collection = self else { return nil }
guard !collection.isEmpty else { return nil }
return collection
return (self?.isEmpty ?? true) ? nil : self
}
}

Expand Down

0 comments on commit 6ed83b7

Please sign in to comment.