I have recently added an “Estimated Reading Time” value to the posts on this website. I decided to implement this after I read David Michael Ross’ intriguing article, ‘Including an “estimated reading time” lowered my bounce rate by 13%.’ (dead link - cached version)
I added the following Ruby code to my default.rb
file:
def reading_time(post)
words = post.split.size
wpm = 200
m = (words / wpm).ceil
s = (words % wpm / (wpm / 60)).floor
if (s > 0)
m += 1
end
est = (m == 1 ? 'about a minute': '~' + m.to_s + ' minutes')
return est
end
The code is really simple. It counts the number of words in the document and, assuming the average reading speed of 200 words-per-minute, calculates the time taken —
I don’t display the seconds in the result. I don’t see any value in learning that the estimated reading time is “3 minutes and 37 seconds”, it is easier to cognitively parse “around 4 minutes”.
When we call this function, we need to pass in the raw Markdown content for the word-count and display the result on our page. We do this by adding something like the following to the appropriate layout file:
<span class="estReadTime">Estimated reading time: <%= reading_time(item.raw_content) %></span>
That’s all there is to it.