Code Inspector
Paste code and the Inspector names the language, counts lines and functions, finds recursion and estimates the time complexity, then shows the evidence behind each answer. JavaScript and Python are parsed and run; 14 other languages are recognised and estimated from their keywords.
How to Use
- Paste a snippet into the box, or pick one of the five presets. The Fibonacci preset is already inspected when the page opens.
- Press Inspect (Ctrl+Enter). The readouts show the language with its confidence, the lines of code, the number of functions and the estimated complexity.
- Read the chart: your estimate is the bold curve, drawn against O(1) up to O(2ⁿ) for inputs of 1 to 16, so you can see how fast the work grows.
- Check the cards for the summary, the language ranking, recursion, issues (such as an unmemoized recursive function) and, for JavaScript and Python, the program’s real output.
- Scroll to Show Work to see every clue that picked the language and the reason given for each loop and each recursive function. Visualize this opens JavaScript or Python in the Execution Visualizer.
Worked Example
Exponential recursion. fib(n) returns fib(n - 1) + fib(n - 2): two calls to itself, each on an argument only one or two smaller. The Inspector finds the 2 self-calls and reports O(2ⁿ). Counting the calls by hand: fib(n) makes 1 + calls(n − 1) + calls(n − 2) calls, which works out to 2 × F(n + 1) − 1, so fib(10) makes 2 × 89 − 1 = 177 calls and fib(15) makes 2 × 987 − 1 = 1,973. The run confirms it: fib(10) prints 55 after 712 recorded steps. Store each result in a memo object and the same code becomes O(n), which the Inspector also recognises.
Halving. The Python binary search keeps lo and hi and looks at mid = (lo + hi) // 2. Searching [1, 3, 5, 7, 9, 11, 13] for 9: mid = 3 (value 7, too small, so lo = 4), mid = 5 (value 11, too big, so hi = 4), mid = 4 (value 9, found). That is 3 passes for 7 items. Because the range halves every pass, a million items need at most 20 passes (220 = 1,048,576), so the Inspector reports O(log n).
The common mistake: nested loops are not automatically O(n²). In the moving-average preset the outer loop visits each of the 6 readings and the inner loop for (let k = 0; k < 3; k++) always runs 3 times: 6 × 3 = 18 additions, and 12 readings would need 36. The work doubles when the input doubles, so it is O(n), and the Inspector says so because both ends of the inner loop are fixed numbers. A sharper case from Sedgewick and Wayne’s Algorithms (section 1.4 exercises): an outer loop that halves n from N = 64, around an inner loop that runs n times, does 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127 steps, not 64 × 64 = 4,096 or 64 × 7 = 448. That sum stays under 2N, so it is linear; the Inspector spots this pattern too and explains it as a geometric series.
Show Work
How It Works
Two depths of analysis. JavaScript and Python are parsed by the Code Lab’s stepping engine into a syntax tree, so the Inspector knows each loop’s exact bounds and which functions call which, and it runs the program to show the output. TypeScript, Java, C, C++, C#, PHP, Ruby, Go, Rust and Bash are read for loop keywords (for, while, foreach, .each), brace or indentation nesting, and functions that mention their own name; that estimate is labelled “keywords only”. SQL, HTML, CSS and JSON are identified and routed, with no complexity.
What a static heuristic cannot tell. It reads the shape of the code, not what the code does with real data. It will call a two-pointer scan O(n²) when the inner pointer never moves back and the true cost is O(n); it assumes a while loop can run n times unless it sees halving; it treats a library call such as sort() or indexOf() as one step although each costs O(n) or more; and it does not see early returns that depend on the input. Each answer comes with its evidence in Show Work so you can check the reasoning rather than trust a label.
Background: Order of Growth and Language Detection
The idea behind the complexity estimate is the order of growth. Sedgewick and Wayne’s Algorithms (4th edition, section 1.4, Analysis of Algorithms) works with approximations of the form g(N) ~ a·f(N) where f(N) = Nb logc N, and observes that because programs are built from a few structural primitives (statements, conditionals, loops, nesting and method calls), the order of growth is very often one of just a few functions of N. Their examples include a brute-force 3-sum with order of growth N³, a faster version in N² log N, and the exercise quoted in the Worked Example, whose answers are “linear (N + N/2 + N/4 + …)”, “linear (1 + 2 + 4 + 8 + …)” and “linearithmic (the outer loop loops lg N times)”.
The O, Ω and Θ notations themselves are the subject of Donald E. Knuth’s note “Big Omicron and big Omega and big Theta”, ACM SIGACT News 8, April 1976, pages 18–24 (doi:10.1145/1008328.1008329). This tool reports O, an upper-bound estimate for the worst case.
Language detection by clues is the approach large code hosts use as one step of several. GitHub’s open-source Linguist library tries, in order, a Vim or Emacs modeline, a commonly used filename, a shell shebang, the file extension, an XML header, a man-page section, heuristics, and finally a naïve Bayesian classifier. A pasted snippet has no filename or extension, so the Inspector relies on the last two kinds of evidence: hand-written heuristics with weights, plus a shebang when one is present.
About This Tool
The Code Inspector tells you what a piece of code is and roughly what it costs: the language and how sure it is, lines of code, functions, loops and their nesting, recursion, an estimated Big-O with a chart of how it grows, common issues, and for JavaScript and Python the real output. Every answer comes with the evidence that produced it.
Most complexity explainers give a label; this one shows its working and is open about where a static estimate fails. It also connects to the rest of Utilities Bunker: JavaScript and Python open in the Execution Visualizer, and code that touches JSON, regular expressions, Base64, hashing, URLs, colours or number bases links the tool for that job. Everything runs in your browser; nothing you paste is uploaded or stored.
It is for students learning Big-O, people preparing for coding interviews, teachers marking algorithm exercises, and anyone handed a snippet in a language they do not recognise.
Related tools: Code Execution Visualizer, Coding Lessons, and Sorting Algorithm Visualizer.
Frequently Asked Questions
How does it decide which language the code is?
It checks the text against a table of clues, each worth a few points: console.log( is 4 points for JavaScript, def name(…): is 4 for Python, #include <stdio.h> is 6 for C, System.out.println is 6 for Java, and so on for 16 languages. The language with the most points wins, and the confidence is its share of all points. Show Work lists every clue that matched, so a 73% TypeScript guess shows exactly which JavaScript clues also fired.
How does it estimate Big-O?
For JavaScript and Python it parses the code and classes each loop: fixed numbers at both ends (k < 3) are constant, a counter that is halved or doubled is log n, anything else grows with n. Nested loops multiply, code in sequence takes the larger cost, and calls to your own functions are costed in. A function that calls itself once is O(n); twice with an argument that only shrinks by one or two is O(2ⁿ); on halves of the input it is O(log n) or O(n log n). Other languages get the same rules from their loop keywords only.
Can it be wrong?
Yes, and it says how. It reads the shape of the code, so it cannot see amortised work: a two-pointer loop where the inner while only ever moves forward is O(n) overall, but the Inspector reports O(n²) because the loops are nested. It also assumes every loop that depends on the input can run n times, ignores early exits that depend on the data, and treats library calls such as sort() as one step. Treat the answer as a well-reasoned first estimate, then check it.
Does it run my code?
JavaScript and Python in the Code Lab’s teaching subset are run in your browser by the same engine as the Execution Visualizer, and the output is shown (fib(10) prints 55 after 712 steps). Other languages are only read, never run. Nothing you paste is uploaded.
Why does SQL say n/a?
SQL describes the result you want, not the steps to get it. The database’s query planner chooses how to fetch the rows, so the cost depends on indexes and table sizes: a lookup on an indexed column can touch a handful of rows, the same query without an index reads every row. HTML, CSS and JSON are not programs at all. The Inspector still identifies them and links the matching tool.
How do I use the Code Inspector?
Simply type your numbers and read the result, which refreshes the instant you change something. There is nothing to submit and nothing to wait for.
Is it free? Does it work without internet?
Yes to both. It is free with no sign-up, and once the page has loaded it keeps working even with no internet.
Where does my data go?
Nowhere — every calculation runs on your own device. Nothing you enter is uploaded, logged, or stored.
Common Use Cases
Spot exponential recursion
Paste a naive fib(): it is flagged O(2ⁿ) with 2 self-calls per call (177 calls for fib(10)) and a suggestion to memoize, which makes it O(n).
Check an interview answer
Binary search over 7 sorted numbers finds 9 in 3 passes; the Inspector labels the while loop O(log n) because the range halves around mid.
Name an unknown snippet
A C file scores 12 points (#include <stdio.h> 6, printf 2, int main 2+2) and comes out C at 83%, with C++ as the runner-up.
Avoid the nested-loop trap
A moving average with an inner loop of k < 3 does 18 additions for 6 readings and 36 for 12: O(n), not O(n²).
Hand off to the right tool
JSON.stringify links the JSON Formatter, a SELECT query links the SQL Formatter, and JavaScript or Python opens in the Execution Visualizer.
Last updated: