Sometimes one hit is all I need: does this huge log contain the error at all, or what’s the first line where it showed up. Plain grep still reads every last byte of the file to give you all the other matches you didn’t ask for.
-m NUM (long form --max-count) makes grep stop reading a file after NUM matching lines:
grep -n -m 1 'Out of memory' /var/log/syslog
That gives you the first out-of-memory line with its line number and then grep quits. On a big file that’s a lot faster than reading to the end. It also works the other way around with tac, which reverses the file, so tac /var/log/syslog | grep -m 1 'Out of memory' shows the most recent one.
It combines well with -c too. grep -c -m 10 counts up to ten matching lines and stops, which is enough to answer “is this happening a lot?” without counting a million lines.
Two caveats. The limit is per file, so with -r or several file names you get up to NUM matches from each file, not NUM in total. And it counts matching lines, not matches: a line with the word three times still counts as one.
Further reading: grep(1) manual page
Going deeper: Run degoog on Unraid as your own search page, compared with SearXNG and 4get