Reddit uses markdown for all of its posts and comments and so I needed a way to parse and render markdown not only well but fast. In the two years I’ve been working on this app of and on I’ve went through multiple differently libraries before I decided it’d be worthwhile to actually go and find the best one once and for all instead of just trying random ones, and so I decided to benchmark the top five I could find.
TL;DR: Use CocoaMarkdown. Skip to the end for a summary.
Test Methodology⌗
I’m collecting two bits of data:
- How long it takes to complete 1000 full cycles (source text to display)
- How much memory is uses max (and how much is leaked)
The library needs to be able to either take its source as HTML or markdown. I can do this because Reddit actually (for whatever reason) provides both HTML and markdown with every API request, wasting user’s bandwidth everywhere.
To test timing I used the following methods to time a code block:
Objective C:
-(void)totalRuntimeForMethod:(NSString*)method block:(void(^)(void))block;
{
extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
int iterations = 1000;
uint64_t t = dispatch_benchmark(iterations, ^{
@autoreleasepool {
block();
}
});
NSLog(@"Runtime for %i iterations (ms) %@: %f", iterations, method, t/1000000.0*iterations);
}
Swift:
func measure(_ title: String, block: (() -> ())) {
let startTime = CFAbsoluteTimeGetCurrent()
let iterations = 500
for _ in 0..<(iter