Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7852857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:31:15+00:00 2026-06-02T19:31:15+00:00

I am needing to iterate over some large arrays and store them in backbone

  • 0

I am needing to iterate over some large arrays and store them in backbone collections from an API call. What is the best way to do this without making the loop cause the interface to become unresponsive?

The return of the ajax request also blocks as the data returned is so large. I figure that I could split it up and use setTimeout to make it run asynchronously in smaller chunks but is there an easier way to do this.

I thought a web worker would be good but it needs to alter some data structures saved on the UI thread. I have tried using this to do the ajax call but when it returns the data to the UI thread there is still a time when the interface is unresponsive.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:31:16+00:00Added an answer on June 2, 2026 at 7:31 pm

    You have a choice of with or without webWorkers:

    Without WebWorkers

    For code that needs to interact with the DOM or with lots of other state in your app, you can’t use a webWorker so the usual solution is to break your work into chunks do each chunk of work on a timer. The break between chunks with the timer allows the browser engine to process other events that are going on and will not only allow user input to get processed, but also allow the screen to draw.

    Usually, you can afford to process more than one on each timer which is both more efficient and faster than only doing one per timer. This code gives the UI thread a chance to process any pending UI events between each chunk which will keep the UI active.

    function processLargeArray(array) {
        // set this to whatever number of items you can process at once
        var chunk = 100;
        var index = 0;
        function doChunk() {
            var cnt = chunk;
            while (cnt-- && index < array.length) {
                // process array[index] here
                ++index;
            }
            if (index < array.length) {
                // set Timeout for async iteration
                setTimeout(doChunk, 1);
            }
        }    
        doChunk();    
    }
    
    processLargeArray(veryLargeArray);
    

    Here’s a working example of the concept – not this same function, but a different long running process that uses the same setTimeout() idea to test out a probability scenario with a lot of iterations: http://jsfiddle.net/jfriend00/9hCVq/


    You can make the above into a more generic version that calls a callback function like .forEach() does like this:

    // last two args are optional
    function processLargeArrayAsync(array, fn, chunk, context) {
        context = context || window;
        chunk = chunk || 100;
        var index = 0;
        function doChunk() {
            var cnt = chunk;
            while (cnt-- && index < array.length) {
                // callback called with args (value, index, array)
                fn.call(context, array[index], index, array);
                ++index;
            }
            if (index < array.length) {
                // set Timeout for async iteration
                setTimeout(doChunk, 1);
            }
        }    
        doChunk();    
    }
    
    processLargeArrayAsync(veryLargeArray, myCallback, 100);
    

    Rather than guessing how many to chunk at once, it’s also possible to let elapsed time be the guide for each chunk and to let it process as many as it can in a given time interval. This somewhat automatically guarantees browser responsiveness regardless of how CPU intensive the iteration is. So, rather than passing in a chunk size, you can pass in a millisecond value (or just use an intelligent default):

    // last two args are optional
    function processLargeArrayAsync(array, fn, maxTimePerChunk, context) {
        context = context || window;
        maxTimePerChunk = maxTimePerChunk || 200;
        var index = 0;
    
        function now() {
            return new Date().getTime();
        }
    
        function doChunk() {
            var startTime = now();
            while (index < array.length && (now() - startTime) <= maxTimePerChunk) {
                // callback called with args (value, index, array)
                fn.call(context, array[index], index, array);
                ++index;
            }
            if (index < array.length) {
                // set Timeout for async iteration
                setTimeout(doChunk, 1);
            }
        }    
        doChunk();    
    }
    
    processLargeArrayAsync(veryLargeArray, myCallback);
    

    With WebWorkers

    If the code in your loop does not need to access the DOM, then it is possible to put all the time consuming code into a webWorker. The webWorker will run independently from the main browser Javascript and then when its done, it can communicate back any results with a postMessage.

    A webWorker requires separating out all the code that will run in the webWorker into a separate script file, but it can run to completion without any worry about blocking the processing of other events in the browser and without the worry about the “unresponsive script” prompt that may come up when doing a long running process on the main thread and without blocking event processing in the UI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself needing to call back into the JVM from arbitrary native threads
I am needing to transfer some logs which were timestamped in ticks to an
I am needing some information on including files in PHP classes. E.G. include Foo2.php;
I am needing some help with SQL syntax. Say I have a members table,
I'm needing an excel formula to output the mineral name from the example list
Questions needing answers : Does the finalizer of the client side ServicedComponent call ServicedComponent.DisposeObject
I'm needing some help in figuring out how to create a leftSpine function in
I'm needing to troubleshoot some old VB6 code and I'm confused about the use
I'm needing some help with mod rewriting. I have been looking online for the
Needing some instructions on Xpath. I have something along the following lines in terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.