-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantiBreakingSystem.js
40 lines (34 loc) · 1.18 KB
/
antiBreakingSystem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
How can we achieve ABS(anti breaking system) kind of thing for heavy calculation function in JS
i.e to perform heavy calculation in chunks
*/
function performHeavyCalculation(data) {
return new Promise((resolve, reject) => {
let result = 0;
let currentIndex = 0;
function processChunk() {
console.log("processChunk")
const chunkSize = 1000; // Adjust as needed
for (let i = 0; i < chunkSize; i++) {
if (currentIndex >= data.length) {
resolve(result);
return;
}
result += data[currentIndex];
currentIndex++;
}
// Process next chunk asynchronously // another thread instead of the main thread
setTimeout(processChunk, 0);
}
processChunk();
});
}
// Example usage
const dataArray = Array.from({ length: 1000000 }, () => Math.random()); // Generate an array of 10,000 random numbers
performHeavyCalculation(dataArray)
.then(result => {
console.log('Calculation result:', result);
})
.catch(error => {
console.error('Error:', error);
});