Laravel Chunk vs. Cursor

I’ve been fighting memory limits more often than I thought I would have to. Giving me the thought that perhaps I’m building my query wrong. But there are a few helpful helpers that can keep the memory low. Two of them being chunk() and cursor().

This speed test is brought to you by @ryo511. His explantation is neat and easy to digest.


  • cursor (): High Speed
  • chunk (): Constant Memory Usage

10,000 records

Time (sec)Memory (MB)
get ()0.17twenty two
chunk (100)0.38Ten
chunk (1000)0.1712
cursor ()0.1614

100,000 records

Time (sec)Memory (MB)
get ()0.8132
chunk (100)19.9Ten
chunk (1000)2.312
chunk (10000)1.134
cursor ()0.545

What is Chunk and Cursor?

For a quick overview of chunk() and cursor() @JeanMarcos has a simplified answer.

Chunk: It will “paginate” your query, this way you use less memory.

  • Uses less memory
  • It takes longer
public function getData() {
    Contact::chunk(1000, function ($contacts) {
        foreach ($contacts as $contact) {
            //rest of your code...

Cursor: You will use PHP Generators to search your query items one by one.

  • It takes less time
  • Uses more memory
public function getData() {
    foreach (Contact::cursor() as $contact) {
        //rest of your code...

Leave a Comment