Eating a heart-healthy diet beginning in your 20s may provide brain benefits in middle age, new research suggests.

The study, in Neurology, ranked 2,621 people on their degree of adherence to three different diets considered to be good for the heart. All emphasize vegetables, fruits and whole grains and minimize saturated fat consumption: the Mediterranean diet, which involves mainly plant-based foods and moderate alcohol intake; a research-based diet plan that rates food groups as favorable or not; and the DASH diet, which stresses low-sodium foods.

Researchers tracked their diet compliance at ages 25, 32 and 45, and tested mental acuity at 50 and then again at 55.

Those who adhered most strictly to the Mediterranean or the food group diet scored higher than those who did not, especially on tests of executive function, which involves organizing and planning. After adjusting for many health and behavioral factors, people with the strictest adherence to these diets had a 46 to 52 percent lower risk of poor cognitive function. But adherence to the DASH diet, which does not consider alcohol consumption, was not associated with cognitive test scores.