0
0
PHPprogramming~5 mins

Input validation vs sanitization in PHP - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Input validation vs sanitization
O(n)
Understanding Time Complexity

We want to understand how the time it takes to check and clean user input grows as the input size grows.

How does the cost of validating and sanitizing input change when the input gets longer?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


function validateInput(array $inputs): bool {
    foreach ($inputs as $input) {
        if (!preg_match('/^[a-zA-Z0-9]+$/', $input)) {
            return false;
        }
    }
    return true;
}

function sanitizeInput(array $inputs): array {
    $cleaned = [];
    foreach ($inputs as $input) {
        $cleaned[] = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
    }
    return $cleaned;
}
    

This code checks if each input only has letters and numbers, then cleans each input to make it safe for display.

Identify Repeating Operations
  • Primary operation: Looping through each input item once in both validation and sanitization.
  • How many times: Once per input element, so the number of times equals the number of inputs.
How Execution Grows With Input

As the number of inputs grows, the time to check and clean them grows in a straight line.

Input Size (n)Approx. Operations
10About 10 checks and 10 cleans
100About 100 checks and 100 cleans
1000About 1000 checks and 1000 cleans

Pattern observation: The work grows evenly with the number of inputs; doubling inputs doubles work.

Final Time Complexity

Time Complexity: O(n)

This means the time to validate and sanitize grows directly with the number of inputs.

Common Mistake

[X] Wrong: "Validation and sanitization take constant time no matter how many inputs there are."

[OK] Correct: Each input must be checked and cleaned, so more inputs mean more work.

Interview Connect

Understanding how input handling scales helps you write secure and efficient code, a skill valued in many coding tasks.

Self-Check

"What if we validated inputs using nested loops to compare each input with every other input? How would the time complexity change?"