optonaut / ActiveLabel.swift

UILabel drop-in replacement supporting Hashtags (#), Mentions (@) and URLs (http://) written in Swift
MIT License
4.47k stars 686 forks source link

Performance issues when used in dequeuing scroll views #146

Closed OscarApeland closed 7 years ago

OscarApeland commented 7 years ago

When using this in my UICollectionView, I experienced slight lagging due to ActiveLabel's NSRegularExpression-handling. Allocating a new NSRegularExpression every time you want to detect hashtags, url's and mentions is very expensive (Up to 45ms on iPhone 7, blocking the main thread.). I solved this in my case by making static instances of the expressions, like this.

static let hashtagMatcher = try! NSRegularExpression(pattern: RegexParser.hashtagPattern, options: [.caseInsensitive])

static let mentionMatcher = try! NSRegularExpression(pattern: RegexParser.mentionPattern, options: [.caseInsensitive])

static let urlMatcher = try! NSRegularExpression(pattern: RegexParser.urlPattern, options: [.caseInsensitive])

That improved it a whole lot. I don't know how you should properly handle custom data detectors like this, so I won't submit a pull request. This issue is ment as a headsup/suggstion/solution for other people experiencing the same thing.

Look at http://stackoverflow.com/questions/41705728/optimize-nsregularexpression-performance for more details

polqf commented 7 years ago

Hi @Coocooso, this is a duplicate of https://github.com/optonaut/ActiveLabel.swift/issues/114

If you take a look at RegexParser.swift, it is no longer like this.

My fault for not creating a new release. Gonna create it straight away!

Thanks for the heads up đŸ˜„

polqf commented 7 years ago

screen shot 2017-01-24 at 10 43 30