Late nights and lax bedtime routines can blunt young children's minds, research suggests.
The findings on sleep patterns and brain power come from a UK study of more than 11,000 seven-year-olds.
Youngsters who had no regular bedtime or who went to bed later than 21:00 had lower scores for reading and maths.
Lack of sleep may disrupt natural body rhythms and impair how well the brain learns new information say the study authors.
They gathered data on the children at the ages of three, five and then seven to find out how well they were doing with their learning and whether this might be related to their sleeping habits.
Erratic bedtimes were most common at the age of three, when around one in five of the children went to bed at varying times.
By the age of seven, more than half the children had a regular bedtime of between 19:30 and 20:30.
Overall, children who had never had regular bedtimes tended to fare worse than their peers in terms of test scores for reading, maths and spatial awareness.
The impact was more obvious throughout early childhood in girls than in boys and appeared to be cumulative.
The researchers, led by Prof Amanda Sacker from University College London, said it was possible that inconsistent bedtimes were a reflection of chaotic family settings and it was this, rather than disrupted sleep, that had an impact on cognitive performance in children.
"We tried to take these things into account," said Prof Sacker.
The children with late and erratic bedtimes came from more socially disadvantaged backgrounds and were less likely to be read to each night and, generally, watched more TV - often on a set in their own bedroom.
After controlling for such factors, the link between poorer mental performance and lax bedtimes remained.
The findings are published in the Journal of Epidemiology and Community Health.
Prof Sacker said: "The take-home message is really that routines really do seem to be important for children.
"Establishing a good bedtime routine early in childhood is probably best, but it's never too late."
She said there was no evidence that putting children to bed much earlier than 19:30 added anything in terms of brain power.
Dr Robert Scott-Jupp of the Royal College of Paediatrics and Child Health said: "At first glance, this research might seem to suggest that less sleep makes children less intelligent, however, it is clearly more complicated than that.
"While it's likely that social and biological brain development factors are inter-related in a complex way, in my opinion, for schoolchildren to perform their best, they should all, whatever their background, get a good night's sleep."