jsdw/angu

Nested "if" code results in exponential execution time

Opened this issue · 0 comments

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.