TORONTO -- While it is known that a lack of sleep can lead to severe health problems, much about the purpose and function of rest has remained a mystery.

Now, researchers at the University of California, Los Angeles (UCLA) say they have discovered the reason behind why people need sleep and how it shifts early in life.

According to the new study, sleep helps build the brain’s infrastructure until the age of about 2-and-a-half. After that, researchers say sleep takes on an "entirely new decluttering role," focusing on brain maintenance and repair rather than brain-building.

"Sleep is as important as food and it's miraculous how well sleep matches the needs of our nervous system. From jellyfish to birds to whales, everyone sleeps. While we sleep, our brains are not resting," the study's senior author and UCLA professor Gina Poe said in a press release.

The study's findings were published Friday in the journal Science Advances.

Researchers say the brain grows "very rapidly" in the first two years of life, and during REM sleep -- when vivid dreams occur -- a young brain is busy building and strengthening the synapses that connect neurons to one another and facilitate communication.

"Don't wake babies up during REM sleep -- important work is being done in their brains as they sleep," Poe said.

After two-and-a-half years, however, the study found that the primary purpose of sleep switches to brain maintenance. Sleep maintains this role for the remainder of human life.

Researchers say this transition corresponds to changes in brain development.

The study found that all animals naturally experience a certain amount of neurological damage during waking hours, such as damaged genes and proteins within neurons, which can build up and cause brain disease. The study reported that nearly all of this brain repair occurs during sleep.

"I was shocked how huge a change this is over a short period of time, and that this switch occurs when we’re so young," senior author and UCLA professor Van Savage said in the release.

"It's a transition that is analogous to when water freezes to ice."

The researchers say their study is the most comprehensive statistical analysis of sleep to date. The team, which included scientists with expertise in neuroscience, biology, statistics and physics, looked at data from more than 60 sleep studies involving humans and other mammals.

They examined sleep throughout development including total sleep time, REM sleep time, brain size and body size. They then built and tested a mathematical model to explain how sleep changes with brain and body size.

The study’s results were "remarkably consistent," finding that all species experienced a dramatic decline in REM sleep when they reached the human developmental equivalent of approximately 2-and-half years old.

According to the study, the fraction of time spent in REM sleep before and after that age was roughly the same, whether looking at rabbits, rats, pigs or humans.

The study reported that newborns spend about 50 per cent of their sleep time in REM sleep, but that falls to approximately 25 per cent by the age of 10 and continues to decrease with age. Adults older than 50 spend roughly 15 per cent of their time asleep in REM and have five REM cycles during a full night of sleep with a few dreams in each cycle.

Poe, who has conducted sleep research for more than 30 years, explained that the significant decline in REM sleep after the age of two happens just as the major change in the function of sleep occurs.

She said a chronic lack of sleep can contribute to long-term health problems such as dementia and other cognitive disorders, diabetes and obesity. Poe added that when you start to feel tired, you should not try to fight it. Instead, just go to bed.

"A good night's sleep is excellent medicine. And it's free," Poe said.

Researchers say a regular seven-and-a-half hours of sleep a night is normal for most adults but children need more, while babies need roughly twice as much sleep as adults.

"I fought sleep and pulled all-nighters when I was in college, and now think that was a mistake. I would have been better off with a good night’s sleep," Savage said. "Now when I feel tired, I don't have any guilt about sleeping."