Big O Calc

Calculate the time and space complexity of your code using Big O notation.

code
0 / 1,500

Paste your code above and click Calculate to analyze its time and space complexity.

How to Use This Tool

  1. Paste your code into the editor above. Supports JavaScript, Python, Java, C++, and more.
  2. Click Calculate to analyze the time and space complexity using Big O notation.
  3. Review the result — you'll get a step-by-step breakdown of how the complexity was determined.

Tip: Keep your code under 1,500 characters for best results. Focus on the core algorithm rather than boilerplate code.

Common Complexity Classes

O(1)

Constant

Array access, hash lookup

O(log n)

Logarithmic

Binary search

O(n)

Linear

Single loop, linear search

O(n log n)

Linearithmic

Merge sort, quick sort

O(n²)

Quadratic

Nested loops, bubble sort

O(2ⁿ)

Exponential

Recursive Fibonacci

Learn the details in our comprehensive guide.

Popular Examples

Binary Search

O(log n)
function binarySearch(arr, target) {
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return -1;
}

Halves the search space each iteration → logarithmic time, constant space.

Two Sum (Brute Force vs Hash Map)

O(n²) O(n)
// Brute force: O(n²)
function twoSum(nums, target) {
  for (let i = 0; i < nums.length; i++)
    for (let j = i + 1; j < nums.length; j++)
      if (nums[i] + nums[j] === target) return [i, j];
}

// Optimized: O(n)
function twoSumFast(nums, target) {
  const map = new Map();
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (map.has(complement)) return [map.get(complement), i];
    map.set(nums[i], i);
  }
}

A hash map trades O(n) space for O(n²) → O(n) time improvement.

Merge Sort

O(n log n)
function mergeSort(arr) {
  if (arr.length <= 1) return arr;
  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));
  return merge(left, right);
}

Divides array into halves (log n levels), merges n elements per level.

See more on the examples page.

What is Big O Notation?

Big O notation is a mathematical notation used to describe the performance or complexity of an algorithm. It specifically describes the worst-case scenario and helps you understand how the runtime or space requirements grow as the input size increases.

Think of it as a way to answer: "If I double my input, how much slower does my code get?" An O(n) algorithm takes twice as long, an O(n²) algorithm takes four times as long, and an O(log n) algorithm barely notices the difference.

When analyzing complexity, we focus on the rate of growth rather than exact numbers. Constants and lower-order terms are dropped because they become insignificant as the input grows very large. For example, O(2n + 5) simplifies to O(n).

Why Big O Matters

Choosing the right algorithm can mean the difference between a program that finishes in milliseconds and one that takes hours. For example, sorting 1 million items with bubble sort (O(n²)) requires roughly 1 trillion operations, while merge sort (O(n log n)) needs only about 20 million — a 50,000x improvement.

Big O analysis is essential for coding interviews at top tech companies, competitive programming, and building production systems that need to scale. It gives you a shared vocabulary to discuss algorithm efficiency with other engineers.

Start Learning

New to Big O? Our step-by-step tutorial walks you through 16 lessons covering everything from constant time to dynamic programming and graph traversal.

Prefer a reference? Read the comprehensive guide for a complete overview of all complexity classes, or explore real algorithm examples with detailed analysis. Have questions? Check the FAQ.