array of 20m items in javascript

To work with an array of 20 million items in JavaScript, one needs to take into account the memory management and performance considerations to optimize the operations on the array.

  1. Use typed arrays: Typed arrays are designed to handle large arrays efficiently in JavaScript, as they use less memory and allow for faster access to the items.
index.tsx
const arr = new Float64Array(20000000); // Assuming each item is a 64 bit float
80 chars
2 lines
  1. Use chunking or pagination: If the operations on the entire array are too expensive, consider breaking the array into smaller chunks or pages that can be loaded and unloaded as needed.
index.tsx
const chunkSize = 1000000; // Process 1 million items at a time
for (let i = 0; i < arr.length; i += chunkSize) {
  const chunk = arr.slice(i, i + chunkSize);
  // perform operations on chunk
}
194 chars
6 lines
  1. Avoid unnecessary operations: Performing unnecessary operations on the array can take up unnecessary time and resources. Only perform the necessary operations based on the use case.
index.tsx
// Bad example - unnecessary operations on array
for (let i = 0; i < arr.length; i++) {
  arr[i] += 1;
  arr[i] *= 2;
  console.log(arr[i]); // Just printing for example purposes
}

// Good example - only perform the necessary operation
for (let i = 0; i < arr.length; i++) {
  arr[i] *= 1.5;
}
295 chars
12 lines
  1. Use a worker: If the operations on the array are too expensive and blocking the main thread, consider using a web worker to perform the operations in a separate thread. This allows for non-blocking operations and a more responsive UI.
index.tsx
// In the main thread
const worker = new Worker('worker.js');
worker.postMessage({ arr });

// In the worker.js
addEventListener('message', (event) => {
  const { arr } = event.data;
  // perform operations on arr
  postMessage({ resultArr });
});
248 chars
11 lines

gistlibby LogSnag