Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip the function if executing time too long. JavaScript

How can I skip a function if the executing time too long.

For example I have 3 functions :

function A(){
 Do something.. ..
}

function B(){
 Do something.. ..
}

function C(){
 Do something.. ..
}

A();
B();
C();

So for example for some reason function B have infinity loop inside, and keep running. How can I skip function B and go to function C if function B executing too long ?

I tried this but seem not working :

try {
 const setTimer = setTimeOut({ 
  throw new Error("Time out!");
 },500);
 B();
 clearInterval(setTimer);
}
catch(error){
 console.warn(error);
}

But seem not working.


Update 1 : FYI, I didn't do any anti-pattern but function B is something in NPM and I submitted issues to the owner of repo. Just try to dodge the bullet so I have some extra times until the fix. Thanks guys for helping me so far :)

like image 337
trungh13 Avatar asked Jan 23 '19 12:01

trungh13


People also ask

How do you break a function in JavaScript?

Using return to exit a function in javascript Using return is the easiest way to exit a function. You can use return by itself or even return a value.

How do you stop a function when running?

When you click Call the function button, it will execute the function. To stop the function, click Stop the function execution button.

Why does my JavaScript take so long to execute?

In simple terms, a long JavaScript execution time is usually the result of non-optimal code or a large payload (self-hosted or third-party). Optimizing your JavaScript code can help it execute faster, resulting in better page load performance.

How to stop the execution of a function in JavaScript?

To stop the execution of a function in JavaScript, use the clearTimeout () method. This function call clears any timer set by the setTimeout () functions.

How can I reduce JavaScript execution time?

There are a number of ways to reduce JavaScript execution time, such as: Code-splitting is the method of splitting your JavaScript bundle into smaller files so that only the critical code (i.e., code that is necessary for above-the-fold content and user interaction) is executed during initial page load.

How do I abort a JavaScript execution?

Client-side Javascript does not have a native abort function, but there are various alternatives to abort Javascript execution: In a function, simply return false or undefined. Manually throw new Error ("ERROR") in a function. Set a function to run on a timer – var timer = setInterval (FUNCTION, 1000). Then clear it to stop – clearInterval (timer)


2 Answers

setTimeout cannot be used to halt the execution of a function

setTimeout cannot be used to cancel the execution of a function. setTimeout is simply a delay before your function is called. Which simply means, yes you can stop it before it's executed but once it's executed you can't.

Halt by checking the time lapsed (not recommended).

A solution would be to check how much time the function is taking by placing checks a bit everywhere in your function.

function A(mili){
  const end = Date.now() + mili;
  for(let i = 0; i < 10000; i++){
    for(let j = 0; j < 10000; j++){
      if(Date.now() > end){
        console.log(mili + ": Halting A...");
        return;
      }
    }
  }
  
}

A(100); //halted
A(1000); //halted
A(10000); //passed

But this doesn't solve your problem and is not recommended because it requires you to add many "checks" at key points where your function may be taking time too long during execution.

Halt by using function generators

This is assuming you're in a synchronous environement

Function generators is not a perfect solution but it is a better solution.

It requires two easy to understand modifications of your original function.

  1. Add * to your function name
  2. Add yield keyword in key spots in your function that may be deemed "long execution time"

Finally, pass the function you wish to test in some wrapper like the run method provided below.

function* A(){
  for(let i = 0; i < 10000; i++){
    for(let j = 0; j < 1000; j++){
       yield;
    }
  }
  return "some final value";
}

function run(gen, mili){
  const iter = gen();
  const end = Date.now() + mili;
  do {
    const {value,done} = iter.next();
    if(done) return value;
    if(end < Date.now()){
      console.log("Halted function, took longer than " + mili + " miliseconds");
      return null;
    }
  }while(true);
}

console.log("Running...");
const res1 = run(A, 1000);
const res2 = run(A, 5000);
const res3 = run(A, 100);

console.log("run(A,1000) = " + res1); //halted
console.log("run(A,5000) = " + res2); //passed
console.log("run(A,100) = " + res3); //halted
like image 87
kemicofa ghost Avatar answered Oct 19 '22 23:10

kemicofa ghost


In order to know how much time a function takes to execute, you need to execute it and measure. Read about the Halting Problem. Turing basically proves the halting problem unsolvable, which means that there's no programmatic way of determining whether a loop is infinite, or more generally, whether an execution would ever stop.

The issue is that even if you set a threshold, in terms of time, on your function execution, you cannot terminate your function from outside. This way once you get into the infinite loop, you wouldn't be able to terminate it, unless your function internally assumes it can go into an infinite loop, and thus measures its own execution time and terminates if it surpasses it. Even with such an approach, you wouldn't save execution time, as all of your functions would still execute, and some would just terminate after they surpass the provided threshold. This, of course, also assumes that all of the methods you execute are written by you, with the assumptions in mind, and no external methods are used.

like image 38
Konstantin Dinev Avatar answered Oct 20 '22 00:10

Konstantin Dinev