However, as people hit their seventh, eighth and ninth decades, they don't sleep as much or as well - and sleep is no longer linked so much to memory, researchers said.
That raises an "alluring question" - whether improving sleep early in life might delay, or even reverse, age-related changes in memory and thinking, said Michael K Scullin, director of Baylor University's Sleep Neuroscience and Cognition Laboratory.
"It's the difference between investing up front rather than trying to compensate later," said Scullin.
"We came across studies that showed that sleeping well in middle age predicted better mental functioning 28 years later," Scullin said.
More From This Section
One example is that a particular kind of "deep sleep" called "slow-(brain)-wave-sleep" helps memory by taking pieces of a day's experiences, replaying them and strengthening them for better recollection.
By the time people reach middle age, more sleep during the day, such as an afternoon nap, also helps people's memory and protects against its decline - as long they don't skimp on night-time sleep, researchers said.
But as they grow older, people wake up more at night and have less deep sleep and dream sleep - both of which are important for overall brain functioning, Scullin said.
Participants aged 18 to 29 were categorised as young; aged 30 to 60 as middle-aged; and older than 60 as old.
Participants were asked how many hours they typically slept, how long it takes them to go to sleep, how often they wake in the middle of the night and how sleepy they feel during the day.
The research was published in the journal Perspectives on Psychological Science.