Directory listing (autoindex) in Nginx - Time & Space Complexity
When nginx shows a directory listing, it reads the folder contents to display them. We want to understand how the time it takes grows as the number of files increases.
How does the work needed change when there are more files in the directory?
Analyze the time complexity of the following nginx configuration snippet.
location /files/ {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
This configuration enables directory listing for the /files/ path, showing file names and modification times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx reads each file entry in the directory one by one.
- How many times: Once for every file or folder inside the directory.
As the number of files grows, nginx must read more entries to list them all.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reads 10 file entries |
| 100 | Reads 100 file entries |
| 1000 | Reads 1000 file entries |
Pattern observation: The work grows directly with the number of files. Double the files, double the reading work.
Time Complexity: O(n)
This means the time to list files grows in a straight line with the number of files in the directory.
[X] Wrong: "The directory listing time stays the same no matter how many files there are."
[OK] Correct: nginx must read each file entry to show it, so more files mean more reading and more time.
Understanding how directory listing scales helps you reason about server performance and user experience when serving many files. This skill shows you can think about real-world system behavior.
"What if nginx cached the directory entries instead of reading them every time? How would the time complexity change?"