  The ratio of branching fractions and the difference in $CP$ asymmetries of
the decays $B^+\to J/\psi \pi^+$ and $B^+\to J/\psi K^+$ are measured using a
data sample of $pp$ collisions collected by the LHCb experiment, corresponding
to an integrated luminosity of $3~{{\,\mbox{fb}^{-1}}}$ at centre-of-mass
energies of 7 and $8~\mathrm{\,Te\kern -0.1em V}$. The results are
\begin{equation*} {\mathcal{B}(B^+\to J/\psi \pi^+)\over\mathcal{B}(B^+\to
J/\psi K^+)}=(3.83\pm0.03\pm0.03)\times10^{-2}\,, \end{equation*}
\begin{equation*} \mathcal{A}^{CP}(B^+\to J/\psi \pi^+)-\mathcal{A}^{CP}(B^+\to
J/\psi K^+)=(1.82\pm0.86\pm0.14)\times10^{-2}, \end{equation*} where the first
uncertainties are statistical and the second are systematic. Combining this
result with a recent LHCb measurement of $\mathcal{A}^{CP}(B^+\to J/\psi K^+)$
provides the most precise estimate to date of $CP$ violation in the decay
$B^+\to J/\psi \pi^+$, \begin{equation*} \mathcal{A}^{CP}(B^+\to J/\psi \pi^+)
=(1.91\pm0.89\pm0.16)\times10^{-2}. \end{equation*}
