1

I found a similar question about the danger of eval() in the case below, but the answer didn't solve the problem and answered If it was dangerous or not, It only suggested another method. That is why I am asking again hoping to get a new answer. Also It was posted in 2016

So I want to pass a condition as a parameter, I know that this can be a function, but I've read that eval () is 67% faster than new function () {return ...;}, that is why I am using eval ()

This is the code

var a = [0];


function LoopWithDelay (func, delay, param, condition){

	console.log(eval (condition));
	
  if (eval (condition)) {
		func (param);
		setTimeout (LoopWithDelay, delay, func, delay, param, condition);
	}
}


function increment (x){
	x[0] += 5;
}


LoopWithDelay (increment, 1000, a, "a[0]<10" );

When calling the LoopWithDelay() function I am passing the final parameter (condition) as a string so that It is evaluated inside the function eval (condition)

Is It wrong to use eval() in this case ?

[edited]
My main focus is to make this code reusable and eliminate redundancy. That is why I want to pass the condition as a parameter.

Example:
In a test application where the screen flashes at different speeds depending on how much time is left.
If t = 0s flash every 2000ms
If 10s<t<20s flash every 1000s
etc.

12
  • It just looks as u r doing loops in the air to achieve something much simpler. The danger here is unexpected behaviors and difficulty maintaining such code. Give an example to what condition can be. Commented Jun 8, 2020 at 0:05
  • 2
    If your string is fixed then it's safe to be used inside eval. But if your string is fixed, then you don't need to use eval! Provide a real example Commented Jun 8, 2020 at 0:06
  • @ItayMoav-Malimovka This is just a test, this function will be used later to manipulate the transform style of an element Commented Jun 8, 2020 at 0:07
  • 1
    I'd say this is a dangerous game of early optimization. Sure, you could do this to speed up your application, but it's at the possible risk of security issues and less maintainable code. Commented Jun 8, 2020 at 0:08
  • 1
    Also, the comments about the speed are comparing it to passing a string to new Function() in the sense used here, which is similar to eval. This is much different than passing an actual function reference like () => a[0]<10. Commented Jun 8, 2020 at 0:26

1 Answer 1

1

It really looks like you are just trying to make a dynamic test that can react to something that changes in your code at runtime. You would typically do this by passing a function as a parameter, not a string with code to be later "eval()ed". This is the way you typically pass "behavior" or evaluate something that is only available at runtime. And it is very common in javascript. This has the same behavior, but doesn't need eval():

var a = [0];

function LoopWithDelay(func, delay, param, condition) {

  let condition_val = condition()
  console.log(condition_val);

  if (condition_val) {
    func(param);
    setTimeout(LoopWithDelay, delay, func, delay, param, condition);
  }
}


function increment(x) {
  x[0] += 5;
}

// capture `a` in the closure of the passed-in function
LoopWithDelay(increment, 1000, a, () => a[0] < 10);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I edited the question and added how the code is used. Yes the condition may sometimes change at runtime, but the main focus was to eliminate redundancy if it was goin to be used multiple times.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.