Skip to content

Commit

Permalink
improve performance by ~70% when parsing large numbers of phone numbe…
Browse files Browse the repository at this point in the history
…rs (e.g. parsing the phone numbers of an entire contact list)
  • Loading branch information
toblerpwn committed Jul 22, 2014
1 parent acd8790 commit 13cce1c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions libPhoneNumber/NBPhoneNumberUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ @interface NBPhoneNumberUtil () {
@property (nonatomic, strong, readwrite) NSMutableDictionary *i18nPhoneNumberDesc;
@property (nonatomic, strong, readwrite) NSMutableDictionary *i18nPhoneMetadata;

@property (nonatomic, strong) CTTelephonyNetworkInfo *telephonyNetworkInfo;

@end


Expand Down Expand Up @@ -3519,8 +3521,17 @@ - (NBPhoneNumber*)parseWithPhoneCarrierRegion:(NSString*)numberToParse error:(NS

- (NSString *)countryCodeByCarrier
{
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
NSString *isoCode = [[networkInfo subscriberCellularProvider] isoCountryCode];
// cache telephony network info;
// CTTelephonyNetworkInfo objects are unnecessarily created for every call to parseWithPhoneCarrierRegion:error:
// when in reality this information not change while an app lives in memory
// real-world performance test while parsing 93 phone numbers:
// before change: 126ms
// after change: 32ms
if (!self.telephonyNetworkInfo) {
self.telephonyNetworkInfo = [[CTTelephonyNetworkInfo alloc] init];
}

NSString *isoCode = [[self.telephonyNetworkInfo subscriberCellularProvider] isoCountryCode];

if (!isoCode) {
isoCode = UNKNOWN_REGION_;
Expand Down

0 comments on commit 13cce1c

Please sign in to comment.