Big-O Complexity Calculator

Paste Python, JavaScript, Java, C++, C# or Go and get its time and space Big-O, with the line-by-line working, the recurrence and a cheat sheet.

Example algorithms

Paste a complete function for the best reading. Comments and strings are ignored, so a loop written inside a string will not be counted.

Big O cheat sheet

The complexity classes, what each data structure operation costs, and how the common sorting algorithms compare. Fixed reference, independent of the code above.

Complexity classes, slowest-growing first

The nine classes almost every algorithm falls into.

NotationNameGrowthPractical limit
O(1)ConstantNo growthUnbounded
O(log n)Logarithmic+1 step per doubling of nBillions
O(√n)Square root4x input, 2x timeTrillions
O(n)Linear2x input, 2x timeTens of millions
O(n log n)LinearithmicA little steeper than linearMillions
O(n²)Quadratic2x input, 4x timeTens of thousands
O(n³)Cubic2x input, 8x timeHundreds
O(2^n)Exponential+1 element, 2x timeAbout 30–40
O(n!)Factorial+1 element, (n+1)x timeAbout 10–12

Data structure operation costs

Average-case cost per operation, with the space the structure itself occupies.

StructureAccessSearchInsertDeleteSpace
ArrayO(1)O(n)O(n)O(n)O(n)
Linked listO(n)O(n)O(1)O(1)O(n)
StackO(n)O(n)O(1)O(1)O(n)
QueueO(n)O(n)O(1)O(1)O(n)
Hash tableN/AO(1)O(1)O(1)O(n)
Binary search treeO(log n)O(log n)O(log n)O(log n)O(n)
AVL treeO(log n)O(log n)O(log n)O(log n)O(n)
Red-black treeO(log n)O(log n)O(log n)O(log n)O(n)
B-treeO(log n)O(log n)O(log n)O(log n)O(n)
HeapN/AO(n)O(1)O(log n)O(n)

Sorting algorithm comparison

Best, average and worst-case time, the extra space required, and whether equal elements keep their original order.

AlgorithmBestAverageWorstSpaceStable
QuicksortO(n log n)O(n log n)O(n²)O(log n)No
Merge sortO(n log n)O(n log n)O(n log n)O(n)Yes
HeapsortO(n log n)O(n log n)O(n log n)O(1)No
TimsortO(n)O(n log n)O(n log n)O(n)Yes
Bubble sortO(n)O(n²)O(n²)O(1)Yes
Insertion sortO(n)O(n²)O(n²)O(1)Yes
Selection sortO(n²)O(n²)O(n²)O(1)No
Radix sortO(nk)O(nk)O(nk)O(n + k)Yes
Counting sortO(n + k)O(n + k)O(n + k)O(k)Yes
Bucket sortO(n + k)O(n + k)O(n²)O(n)Yes

This analyzer reads code structure — loop nesting, loop step form, recursion shape, known library costs and allocations. It cannot see data-dependent behaviour, library internals, or logic that changes the trip count at runtime. Treat the result as a well-evidenced reading of the code you pasted, and check the derivation against your own understanding.

Did this calculator solve your problem today?

Find the Big-O of your own code

Paste a function and this calculator reads its structure — where each loop starts and how it advances, how a recursive call shrinks its argument, which library calls hide a scan — and reports both the time and the space complexity. Every conclusion is listed against the line that produced it, with what that line adds and, for a recursive function, its recurrence, so you can check the reasoning rather than take a notation on trust. Nothing is uploaded: the analysis runs entirely in your browser.

What Big-O notation actually measures

Big-O describes how the work an algorithm does grows as its input grows, ignoring constant factors and lower-order terms. An O(n²) function is not necessarily slow — on ten elements it may well beat an O(n log n) one — but as n rises the growth rate decides the outcome, and no amount of micro-optimisation changes the class. Time complexity counts basic operations; space complexity counts memory. Both are reported here, because a solution that trades one for the other is only judged fairly when you can see both.

The definition

T(n) = O(f(n)) as n → ∞

How to use the analyzer

1

Paste a function, a loop, or a whole method into the code box. A complete function gives the best reading, because recursion can only be detected when the definition is visible.

2

Check the language under the code box. It is detected automatically, from a bare method as well as a whole file; pick one if a short snippet is ambiguous.

3

Read the time and space complexity directly under the code, then work down the derivation: each line shows what it adds, and a recursive function shows its recurrence.

4

Check the limits panel. Anything the analyzer could not determine is listed there, and it changes how much weight the headline deserves.

5

In How it scales, set an input size to turn the notation into an operation count, and a throughput to see the time and the largest n that fits.

6

Use the Big O cheat sheet below the analyzer for the complexity classes, data structure operations and sorting algorithms at a glance.

When this is useful

Interview preparation

Write a solution, check the complexity you think it has against the one its structure actually implies, and see exactly which line makes it quadratic when the two disagree.

Code review

A function that scans a list inside a loop passes tests and fails in production. Pasting the diff's hot function gives a reviewable, line-referenced reason to ask for a set instead.

Teaching and learning

The derivation makes the abstract rules concrete: sequential blocks take the maximum, nested blocks multiply, and a halving step contributes a logarithm.

Choosing between two implementations

Analyze both and compare the notations and the operation counts at the input size you actually expect, rather than at the asymptote.

Why read the derivation, not just the answer

A notation with no working is unverifiable

Most online Big-O tools send your snippet to a language model and return a bare O(...) with no reasoning. When it is wrong — and on unusual code it will be — nothing on the page tells you so. Here each finding names the line it came from and what it adds to the answer.

The hidden costs are the ones that bite

A slice inside a loop, a string grown with += in Java or Python, a membership test against a list rather than a set, a pop from the front of a list: each looks like one statement and each is a full scan. These are the commonest causes of a function that is quietly quadratic, and each is listed against its line.

Two inputs are not one input

Nested loops over two different collections are O(n × m), not O(n²), and two loops over them one after the other are O(n + m). When the code has two dimensions the analysis names them separately and tells you which variable each one stands for.

Knowing what could not be determined is part of the answer

Data-dependent loops, pivot-based recursion and a list the code keeps growing inside its own loop are stated as limits rather than papered over with a confident guess.

Frequently asked questions

Count how many times each statement runs as a function of the input size. Sequential blocks take the maximum of their costs; nested blocks multiply. A loop that adds a fixed amount to its counter is linear; one that multiplies or divides it is logarithmic. A recursive function is solved by its recurrence: two calls on half the input plus linear work gives n log n, two calls on n − 1 gives 2^n. This calculator applies exactly those rules and shows each application against a line number.

Big O is an upper bound: it says the work grows no faster than the given function. It is most often quoted for the worst case, and that is what this calculator reports — a loop that can exit early is still counted at its full length, and the limits panel says so. The best case is a separate question, usually written with Big Omega (Ω).

Big O is an upper bound (grows no faster than), Big Omega (Ω) a lower bound (grows at least as fast as), and Big Theta (Θ) both at once, a tight bound. Merge sort is Θ(n log n): never better or worse than n log n. Insertion sort is O(n²) in the worst case but Ω(n) in the best, so no single Θ covers all its inputs. The calculator reports Big O for the worst case.

Python, JavaScript and TypeScript, Java, C and C++, C# and Go. Detection is automatic, from a bare method as well as a whole file, and you can override it. The analysis is structural rather than a full parse, so it also copes with an incomplete snippet — a loop body pasted without its enclosing function still reads correctly.

For the loop, recursion and library shapes it recognises, yes, and it shows its working so you can confirm. It is checked against more than a hundred textbook and interview algorithms in six languages. What it cannot see is behaviour that depends on the data: a loop that usually exits early, a hash table that degrades to a list on adversarial keys, a list the code keeps growing inside its own loop. Those are reported as limits, not folded silently into the answer.

Time complexity counts operations as the input grows; space complexity counts memory. They frequently trade against each other — memoising a recursive function turns exponential time into linear time by spending linear space. Reading only one of them hides the price of the other, which is why both are shown here.

O(1), constant time: the work does not grow with the input at all. Next come O(log n) and O(√n), then O(n). Whether a better class is reachable depends on the problem; no comparison sort beats O(n log n) in the worst case, for example.

The same idea applied to functions near a point or at infinity. In calculus, f(x) = O(g(x)) as x → 0 means |f(x)| ≤ C·|g(x)| close to zero; the error term of a Taylor series, sin x = x − x³/6 + O(x⁵), is the classic use. Algorithm analysis uses the version at infinity: how the work grows as n gets large.

Auxiliary space is the memory an algorithm allocates on top of the input it was handed. An in-place sort is O(1) auxiliary even though the array it sorts is n elements long, because it did not allocate that array. This is the convention used in interviews and in most textbooks.

Because the two loops iterate different collections. Their sizes are independent, so the cost is the product of two separate dimensions and collapsing it to n² would assert a relationship the code does not contain. For the same reason two loops over them one after the other are O(n + m). The badges under the result show which variable each symbol stands for.

No. The analysis runs in your browser. Nothing is sent to a server, which is also why there is no wait for a response and no rate limit.