"Gillingham are a good side and no easy catch so it was a terrific victory," said City's skipper Graham Kavanagh.
"The clean sheet was just as important and while the match was won by half time it was vital that our defence stayed alert."
Earnshaw notched a hat-trick in 24 first half minutes but it was Peter Thorne who opened the floodgates. He floated a far post header beyond the over-worked Jason Brown in the Gills goal and in off the post in the 18th minute.
Earnshaw notched his first three minutes later when the visitors failed to clear a Kavanagh corner.
The pint-sized striker made it 3-0 from a quickly taken Rhys Weston free-kick and then completed his hat-trick from the spot in the final minute of the half when Thorne was brought down by Brown.
There was no way City could keep up that momentum in the second half and it was not until midway that they improved their goal difference even further when Earnshaw reacted quickest to a rebound off the post to beat Brown.
"The clean sheet was the most pleasing part of that performance," said Bluebirds boss Lennie Lawrence.
"It was a good all-round performance and every player contributed to an outstanding victory.
"They were battered today but Andy Hessenthaler is one of the brightest young managers and he certainly made a difference when he came on just before half time."
Cardiff City have undoubtedly announced their arrival in Division One and on this form few would bet against them being in the promotion frame at the end of the season.