DEV Community

Calculate the estimated reading time of an article using JavaScript

Michael Burrows on January 08, 2021

You’ve probably seen displayed on websites like Medium an estimated reading time. This metric helps users decide if they read the article immediate...
Collapse
 
bbarbour profile image
Brian Barbour

Awesome! Thanks for sharing.

Collapse
 
tr11 profile image
Tiago Rangel

Thanks!

With the help of ChatGPT, I created a reading time estimator even more precise than this based on the code you provided. Here's the code if you wanna use it:

            function readingTime(text) {
              const averageWPM = 250;

              const words = text.trim().split(/\s+/);

              const adjustedText = text.replace(/(.)\1+/g, '$1');

              const adjustedSentences = adjustedText.replace(/([.!?])\s*\1+/g, '$1');

              const adjustedCharCount = adjustedSentences.length;

              const adjustedWords = adjustedSentences.trim().split(/\s+/);
              const adjustedWordCount = adjustedWords.length;
              const averageWordLength = adjustedCharCount / adjustedWordCount;

              const adjustedTime = (adjustedCharCount / averageWPM) * (averageWordLength / 5);

              const formattedAdjustedTime = adjustedTime > 1 ? Math.round(adjustedTime) + " min" : "Less than 1 min";

              return formattedAdjustedTime;
            }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
daviddalbusco profile image
David Dal Busco

Interesting share @michaelburrows.xyz 👍

The constant value 225 for wpm is a known general accepted value or what's its source?

Collapse
 
singhnikhurpa profile image
Bhupesh Singh Nikhurpa

According to a speed-reading test sponsored by Staples as part of an e-book promotion, here are the typical speeds at which humans read, and in theory comprehend, at various stages of educational development :

Third-grade students = 150 words per minute (wpm)
Eighth grade students = 250 wpm
Average college student = 450 wpm
Average "high-level exec" = 575 wpm
Average college professor = 675 wpm
Speed readers = 1,500 wpm
World speed reading champion = 4,700 wpm
Average adult = 300 wpm

Collapse
 
daviddalbusco profile image
David Dal Busco

Excellent! Thanks for the metrics 👍.

Proven numbers and code snippets, it seems that I have no more excuses to not add such information to our dashboard 😉.

Collapse
 
reza profile image
Reza Majidi

Medium is using 265 WMP for calculation.

medium.com/blogging-guide/how-is-m...

Collapse
 
tipseason profile image
Tip Season

Hey Michael, Thanks for this tutorial. Recently made a tool to calculate time to read a post. Sharing to help someone trying to estimate time for their blogs.

tipseason.com/reading-time-calculator

Collapse
 
rabiudev profile image
rabiu-dev

I know the split() method splits a string into an array of substrings. But what does the parameter inside this split method do in this code?? split(/\s+/)

Collapse
 
michaelburrows profile image
Michael Burrows

It's a regular expression that splits the string with \s matching single whitespace characters and \s+ matching one or more whitespace characters.

Collapse
 
wilsonibekason profile image
Wilson Ibekason

nice

Collapse
 
rabiudev profile image
rabiu-dev

Thank you kind Sir for your explanation...

Collapse
 
geekreflex profile image
Jerry Nwosu

Thanks for this

Collapse
 
jodaut profile image
José David Ureña Torres

This share helped me a lot. It's exactly what I was looking for. Thanks!

Collapse
 
vigneshv01 profile image
Vignesh Vaidyanathan

Thank you!

Collapse
 
rockykev profile image
Rocky Kev

This is brilliant and so simple. Thanks for sharing this!

Collapse
 
tilakjain123 profile image
Tilak Jain

Thanks for Sharing!