Skip to content

Instantly share code, notes, and snippets.

@vincentorback
Last active February 14, 2019 14:05
Show Gist options
  • Save vincentorback/6b8c6d14ddb65488a1f2 to your computer and use it in GitHub Desktop.
Save vincentorback/6b8c6d14ddb65488a1f2 to your computer and use it in GitHub Desktop.
Reading time / Words per minute
/**
* Get reading time
* @param {String} text The text string to analyze
* @return {Int} Reading time in milliseconds
*/
function getReadingTime(text) {
var totalWords,
wordsPerMillisecond,
totalReadingTime,
wordsPerMinute = 250,
wordsPerMillisecond = wordsPerMinute / 60000;
text = text.replace(/<\/?[^>]+(>|$)/g, ''); // Remove any HTML-tags
totalWords = text.split(/\s+/g).length; // Split up in words
totalReadingTime = Math.floor(totalWords / wordsPerMillisecond);
return totalReadingTime;
}
/**
# Average word per minute
Third-grade students = 150
Eight grade students = 250
Average adult: 300
Average college student = 450
Average “high level exec” = 575
Average college professor = 675
Speed readers = 1,500
World speed reading champion = 4,700
*/
@vincentorback
Copy link
Author

You can do fun stuff like this:

var readingTime = getReadingTime(document.body.textContent);

navigator.getBattery().then(function(battery) {
  if ((readingTime * 1000) > battery.dischargingTime) {
    alert('You won’t be able to finish this article. Charge your battery!');
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment