Chapter 1 · Simulation

Algorithm Complexity Analysis

← Chapter 1

Complexity

Input sizes

Press Run to time the function.

The average running time measured for each input size will be plotted here.

Algorithm Complexity Analysis

Pick or edit a JavaScript function, run it on several input sizes, and compare the measured running times.

How to use

  • Choose a complexity class (O(log n), O(n), O(n log n), O(n²), O(n³)). The editor receives a sample myFunction(size) of that class and the five input sizes are set to suitable presets (except for O(log n), each size is twice the previous one).
  • You may change the function or the sizes. The code must define a function named myFunction whose only argument is the input size.
  • Press Run. First a warm-up measurement is made and discarded; then for each input size the function is run a few times as a warm-up and then repeated for about 40 ms. The chart shows the average time per call in milliseconds (in microseconds if every time is below 1 ms).

Reading the chart

  • For O(n), doubling n should roughly double the time; for O(n²) it should roughly quadruple it; for O(n³) it should multiply it by about eight.
  • For O(log n) the curve flattens quickly: going from 10 to 100,000 adds only a few iterations to the loop.
  • Real measurements are noisy: the JIT compiler, caches, garbage collection and the browser's other work all affect the time. Run the analysis several times and look at the overall trend, not at single points.

Note

The selected code runs locally in your browser. Very large inputs or expensive functions may temporarily slow the page down.