We express the essential norm of a composition operator on the Hardy space $H^2$ as the asymptotic upper bound of a quantity involving the Nevanlinna counting function of the inducing map. There results a complete function theoretic characterization of the compact composition operators on $H^2$. Simlar results holds for the weighted Bergman spaces of the unit disc. As consequences we obtain:
(i) estimates of the essential norm of a composition operator in terms of the angular derivative of its inducing map;
(ii) a new proof of a recently obtained characterization of the compact composition operators on the weighted Bergman spaces; and
(iii) a new proof of a peak set theorem for holomorphic Lipschitz functions.