简体   繁体   中英

Parallel execution of a function | No async await as no awaiting required

Execute a function N number of times in parallel. Here i want to execute function fun in parallel.

function fun(){
    for(let j=0; j < 100; j++){
        console.log("i");
  }
}

var start =  Date.now();
for(let i = 0; i < 100; i++){
        fun();
  //  setTimeout(fun(), 100); --> Tried this way
}
console.log("ending in " + - ( start - Date.now()) / 1000 + 'seconds');

JSFiddle:- https://jsfiddle.net/Gourishankar/p4t6w73x/15/

It seems to be a very basic problem for optimization but i am not able to conclude/find a full proof solution.

-> Does Service worker solves this ? or only way to solve such problem ? -> Is there any other way ?

I have similar real problem where while doing paste on ag-grid setter function is called multiple times with different params. I want to optimize that.

Thanks in advance !

Javascript uses a single-threaded execution model. To do concurrent calculations (ie have more than one thread of execution), your only option is (multiple?) web workers.

The discussion around whether this is an appropriate solution is not covered by this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM