Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested "if" code results in exponential execution time #13

Open
cristianoccazinsp opened this issue Jul 25, 2022 · 0 comments
Open

Nested "if" code results in exponential execution time #13

cristianoccazinsp opened this issue Jul 25, 2022 · 0 comments

Comments

@cristianoccazinsp
Copy link

I've defined a simple language with some operators and when testing a really awful nested if condition code, the execution time jumps from just 3ms (great) to over a second.

For example:

if(answer(8)=="test1" and numeric(7)<3, res0,
if(answer(8)=="test1" and numeric(7)==3, res1,
if(answer(8)=="test1" and numeric(7)==4, res1,
if(answer(8)=="test2" and numeric(7)==1, res0,
if(answer(8)=="test2" and numeric(7)==2, res1,
if(answer(8)=="test2" and numeric(7)==3, res2,
if(answer(8)=="test2" and numeric(7)==4, res2,
if(answer(8)=="test3" and numeric(7)==1, res1,
if(answer(8)=="test3" and numeric(7)==2, res2,
if(answer(8)=="test3" and numeric(7)==3, res3,
if(answer(8)=="test3" and numeric(7)==4, res3,
if(answer(8)=="test4" and numeric(7)==1, res1,
if(answer(8)=="test4" and numeric(7)==2, res3,
if(answer(8)=="test4" and numeric(7)>2, res4, res0))))))))))))))

The answer and numeric calls just lookup the number in a dict/map object defined in the scope.
And the operators are defined as follows:

Screen Shot 2022-07-25 at 18 50 29

Screen Shot 2022-07-25 at 18 52 15

With the following helper function:

const numberFunDefault = (a, b, def, fun) => {
  let va = Number(a?.eval());
  let vb = Number(b?.eval());

  if (isNaN(va)) {
    va = def;
  }
  if (isNaN(vb)) {
    vb = def;
  }

  return fun(va, vb);
};

When debugging in chrome, looks like a lot of time is lost in GC:
Screen Shot 2022-07-25 at 18 54 39

Any ideas what could be improved? Note that these kind of nested ifs will actually not be written like this, but rather as a lookup table which runs really quick, but while testing this seemed like an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant