Abstract A test of lepton flavor universality in B ± → K ± μ + μ − and B ± → K ± e + e − decays, as well as a measurement of differential and integrated branching fractions of a nonresonant B ± → K ± μ + μ − decay are presented. The analysis is made possible by a dedicated data set of proton-proton collisions at s = 13 TeV recorded in 2018, by the CMS experiment at the LHC, using a special high-rate data stream designed for collecting about 10 billion unbiased b hadron decays. The ratio of the branching fractions B ( B ± → K ± μ + μ − ) to B ( B ± → K ± e + e − ) is determined from the measured double ratio R ( K ) of these decays to the respective branching fractions of the B ± → J / ψ K ± with J / ψ → μ + μ − and e + e − decays, which allow for significant cancellation of systematic uncertainties. The ratio R ( K ) is measured in the range 1.1 < q 2 < 6.0 GeV 2 , where q is the invariant mass of the lepton pair, and is found to be R ( K ) = 0.78 − 0.23 + 0.47 , in agreement with the standard model expectation R ( K ) ≈ 1 . This measurement is limited by the statistical precision of the electron channel. The integrated branching fraction in the same q 2 range, B ( B ± → K ± μ + μ − ) = ( 12.42 ± 0.68 ) × 10 − 8 , is consistent with the present world-average value and has a comparable precision.