2

Is there an inbuild method in JavaScript for calculating time complexity of algorithms?

For instance, if I feed in this function:

function solution(A) {
    let smallestInt = 1;

    function existsInArray(val) {
        return A.find((a) => a === val);
    }

    for (let index = smallestInt; index < 1000000; index++) {
        if (existsInArray(index) && !existsInArray(index + 1) &&
            existsInArray(smallestInt)) {
            smallestInt = index + 1
        }
    }
    return smallestInt;
}

The method should be able to tell me that the time complexity of the algorithm is O(N*2) or whichever correct answer would be?

If no inbuild method is available, is there a library with automated methods for achieving this?

Daniel
  • 346
  • 1
  • 9

2 Answers2

2

No. Such a program would solve the halting problem. Which is impossible.

Griffin
  • 13,184
  • 4
  • 29
  • 43
1

Generally no, though one could run multiple simulations on different inputs to come up with a reasonable approximation.

See these three useful discussions:

  1. Are runtime bounds in P decidable? (answer: no)
  2. Programmatically obtaining Big-O efficiency of code
  3. A tool for calculating the big-O time complexity of Java code?
simmer
  • 2,639
  • 1
  • 18
  • 22
  • 1
    Those are some useful links there. Thanks. Following up on these resources provide a general gist that there is no library (yet) that could achieve what am looking for. But the search is not complete. I would want to know how platforms such as [codility](https://app.codility.com/programmers/) performs automatic time-complexity for algorithms written on the platform. – Daniel May 04 '20 at 17:54