Home > Web Front-end > JS Tutorial > How can you iterate over large arrays without blocking the UI thread?

How can you iterate over large arrays without blocking the UI thread?

Linda Hamilton
Release: 2024-11-04 06:38:02
Original
489 people have browsed it

How can you iterate over large arrays without blocking the UI thread?

Non-Blocking Array Iteration Strategies

When iterating over large arrays, it's crucial to avoid blocking the UI thread and compromising user experience. This article explores various strategies for achieving non-blocking iteration without using web workers and with the use of web workers.

Without Web Workers

For code that needs to interact with the DOM, a common solution is to break the iteration into smaller chunks and process them asynchronously using timers. This allows the browser to process other events, keeping the UI responsive.

<code class="javascript">function processLargeArray(array) {
  // Chunk size for processing
  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();
}</code>
Copy after login

Alternately, you can define a more generic function that accepts a callback function for processing each element:

<code class="javascript">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) {
      // Call the callback 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();
}</code>
Copy after login

To avoid guessing the optimal chunk size, you can use an interval-based approach:

<code class="javascript">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) {
      // Call the callback 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();
}</code>
Copy after login

With Web Workers

Web workers offer another solution when the iteration code doesn't need DOM access. The time-consuming code is moved to a separate script file and runs in a worker thread. Upon completion, the worker can post results back to the main thread without hindering event processing.

The above is the detailed content of How can you iterate over large arrays without blocking the UI thread?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template