简体   繁体   English

并行执行功能 无需等待,无需等待

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

Execute a function N number of times in parallel. 并行执行N次功能。 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/ 的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. 我有类似的实际问题,即在ag-grid setter函数上执行粘贴时,使用不同的参数多次调用。 I want to optimize that. 我想优化它。

Thanks in advance ! 提前致谢 !

Javascript uses a single-threaded execution model. Javascript使用单线程执行模型。 To do concurrent calculations (ie have more than one thread of execution), your only option is (multiple?) web workers. 要进行并发计算(即具有多个执行线程),您唯一的选择是(多个?)Web Worker。

The discussion around whether this is an appropriate solution is not covered by this answer. 该答案未涵盖有关这是否是适当解决方案的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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