Calling `count()` in a loop can be a major performance problem in PHP (yea, I know). An easy way to avoid it is to do something like `if ( ++$count >= $resultSize ) { break; }`.

Also this check should be inside the `if` above.