Microelectromechanical systems (MEMS) are miniature devices integrated into a vast range of industrial and consumer applications. Optical MEMS are developed for dynamic spatiotemporal control in lightwave manipulation and communication as modulators, switches, multiplexers, spectrometer, etc. However, they have not been shown to function similarly in sub-nm wavelength regimes, namely, with hard x-rays, as high-brilliance pulsed x-rays have proven powerful for addressing challenges in time-domain science, from energy conversion to neurobiological control. While desirable temporal properties of x-ray pulses can be enhanced by optics, conventional x-ray optics are inherently massive in size, hence, never dynamic. We demonstrate highly ultrafast x-ray optics-on-a-chip based on MEMS capable of modulating hard x-ray pulses exceeding 350 MHz, 103× higher than any other mechanical modulator, with a pulse purity >106 without compromising the spectral brilliance. Moreover, the timing characteristics of the devices can be tuned on-the-fly to deliver optimal pulse properties to create a host of dynamic x-ray instruments and applications, impossible with traditional optics of 109× bulkier and more massive. The advent of the ultrafast optics-on-a-chip heralds a new paradigm of x-ray photonics, time-domain science, and accelerator diagnostics, especially at not only the future-generation light sources that offer coherent and high-frequency pulses but also lab-based facilities that normally do not offer timing structures.