Without doing a log plot, it’s a good guess that this is exponential growth in the node hits.
In any case it is clear that as the string length gets up around 10-12, the parsing time is very long.
First lets limit the node hits to under a 100,000 and see what happens.
console.log();
console.log(' : limit the node hits to < 100,000');
exp = new apgExp(grammar, flags, 100000);
str = 'aaaaa';
try {
for (let i = 0; i < 5; i += 1) {
result = exp.exec(str);
if (result === null) {
throw new Error('limit.js: result should never be null');
}
console.log(` string: (${str.length}): ${str}`);
console.log(` node hits: ${result.nodeHits}`);
console.log(`tree depth: ${result.treeDepth}`);
str += 'a';
}
} catch (e) {
console.log(`NODE HITS EXCEPTION: ${e.message}`);
}