Seven. That seems to be the magic number when it comes to optimal sleep time, according to new research that looked at the connection between sleeping habits and age-related memory loss.

In what's described as the first study of its kind, researchers at the Brigham and Women’s Hospital in Massachusetts found that extreme sleep duration -- either too much or too little -- in middle-aged women led to worse memory later in life.

The same was observed in women who reported erratic sleep patterns: those whose sleep durations changed by more than two hours a day also increased their risk of developing faulty memories later in life compared to those who kept regular sleep schedules.

"Given the importance of preserving memory into later life, it is critical to identify modifiable factors, such as sleeping habits, that may help achieve this goal," said study co-author Elizabeth Devore in a statement.

“Our findings suggest that getting an 'average' amount of sleep, seven hours per day, may help maintain memory in later life and that clinical interventions based on sleep therapy should be examined for the prevention of cognitive impairment."

For the research, scientists looked at the past sleep patterns and memory skills of about 15,260 female nurses aged 70 or older who were participants in the long-running Nurses' Health Study.

Compared to women who'd gotten the recommended seven hours of shut-eye a night, those who'd slept less than five hours or more than nine demonstrated weaker memory skills -- equal to aging two more cognitive years.

Initial findings were presented in 2012 at the Alzheimer's Association International Conference, and the study has now been published in The Journal of the American Geriatrics Society.