javascript - Is there any way to detect the execution time of a synchronous method in js?
迷茫2017-06-15 09:23:26
0
5
880
Specifically, is there a method that can detect the execution time of a synchronization method? If the execution time is too long, I will take the initiative to kill this method.
If it is a method provided by a third-party library, it depends on whether it provides a stop method. If not, there is nothing you can do;
If it involves file i/o/delete/create, you may not be able to stop it, but you can write an inverse function to restore the original state after it reports an error;
If this method is written by yourself and has multiple sub-functions, then you can define a timer inside the function and check whether the time exceeds the allowed value before executing each sub-function. This is a workaround;
If this method is written by you and has only one step, since it is a time-consuming method, then there is probably a loop, so it is okay to check the time each time it loops. . .
From a JS perspective, when executing a synchronous method, the entire application will be blocked in it. At this time, even the callback triggered asynchronously by setTimeout can only be executed in the Event Loop after the execution of the synchronous method is completed, so it cannot be executed in the synchronous method. Check its own execution time and Kill itself.
From the perspective of computer science, the question asked is actually the classic halting problem:
The halting problem (English: halting problem) is a problem in computability theory in logic and mathematics. In layman's terms, the halting problem is the problem of determining whether any program can end its operation within a limited time. This problem is equivalent to the following decision problem: whether there is a program P. For any input program w, it can be judged that w will end within a limited time or loop endlessly.
This problem is NP-hard, which means that there is no algorithm with acceptable time complexity to solve it directly.
But looking beyond the academic algorithm, there are many workarounds in engineering, such as using the process management mechanism of Web Worker or PM2, etc., which will not be discussed here.
Then you can only do multi-threading... http://www.w3school.com.cn/ht... However, how to kill js is also a mystery... In the end...you can directly Wouldn’t it be better to use asynchronous||||
Webworker can execute your calculation logic in another thread, the onmessage method can accept the execution results, and the terminate method can terminate the execution of webworker
var job001DoneIndictor=false;
var myWorker = new Worker('job001.js');
//接收worker传递过来的数据
myWorker.onmessage = function(event){
job001DoneIndictor=true;
};
setTimeout(function(){
if(!job001DoneIndictor){
myWorker.terminate();
myWorker =null;
}
},5000);//设置超时检测时间
This depends on your synchronization method?
If it is a method provided by a third-party library, it depends on whether it provides a stop method. If not, there is nothing you can do;
If it involves file i/o/delete/create, you may not be able to stop it, but you can write an inverse function to restore the original state after it reports an error;
If this method is written by yourself and has multiple sub-functions, then you can define a timer inside the function and check whether the time exceeds the allowed value before executing each sub-function. This is a workaround;
If this method is written by you and has only one step, since it is a time-consuming method, then there is probably a loop, so it is okay to check the time each time it loops. . .
From a JS perspective, when executing a synchronous method, the entire application will be blocked in it. At this time, even the callback triggered asynchronously by setTimeout can only be executed in the Event Loop after the execution of the synchronous method is completed, so it cannot be executed in the synchronous method. Check its own execution time and Kill itself.
From the perspective of computer science, the question asked is actually the classic halting problem:
This problem is NP-hard, which means that there is no algorithm with acceptable time complexity to solve it directly.
But looking beyond the academic algorithm, there are many workarounds in engineering, such as using the process management mechanism of Web Worker or PM2, etc., which will not be discussed here.
Then you can only do multi-threading...
http://www.w3school.com.cn/ht...
However, how to kill js is also a mystery...
In the end...you can directly Wouldn’t it be better to use asynchronous||||
I have been reminded that this is indeed not the solution. Another low-end solution may only be this:
Not necessarily suitable for all businesses, and the dots themselves will make the process slower.
Webworker can execute your calculation logic in another thread, the onmessage method can accept the execution results, and the terminate
method can terminate the execution of webworker