Play is sometimes very slow and other times it's very fast in After effects. Why?
You've answered your own question; when you preview in After Effects it renders first, then plays back. The default setting is that if you hit numpad-0 to initiate a preview it will render the frames it can until it either fills up the available RAM, gets to the end of the preview area (work area or end of comp), or you hit numpad-0 again, at which time it will play back from RAM, theoretically much faster.

In recent iterations of the software, the playback performance has got significantly worse. I don't know why AE can't easily play back even UHD previews from RAM or even from disc at real time or faster, just like every other free media player app can. You get better playback performance if you have a comp dynamically linked in Premiere, it makes no sense. Unfortunately that's just the way it is, and the only thing you can do about it is learn Nuke, Fusion or Blender. 

Some tips for better real-time playback: updating the UI seems to take an inordinate amount of processing power. So having things like the graph editor, Lumetri scopes or even just the timeline visible while previewing will slow it down. I work on two monitors, and if I can't get real-time playback I will maximise (~) a window that doesn't get refreshed during playback, like the tools window, so that it fills the entire monitor, and that improves performance dramatically. Also making sure the comp window is zoomed to 100% will help, and if that doesn't work, lowering the preview resolution ctrl / ⌘shiftj to half or quarter ctrl / ⌘shiftaltj will speed things up.