-3

i'm looking to see which is faster?

I have a lot of users on the site at any given time and a lot of content to echo out.

Which would be faster?:

case 1:

echo htmlspecialchars($_POST['name'], ENT_QUOTES);

case 2:

file 1:

function cleanText($string) {
    $newString = htmlspecialchars($string, ENT_QUOTES);
    return $newString;
}

echo cleanText($_POST['name']);
ThatGuyInIT
  • 2,239
  • 17
  • 20
John Jaoill
  • 159
  • 2
  • 9

1 Answers1

1

A few general thoughts:

  • The case with the function is going to be very very slightly slower. It does the same thing as the non-function line, except a function needs to be called.
  • The difference is so minuscule, that you should not worry about it. Calling functions is so fast that you should probably never make the trade-off of making less functions for the sake of speed.
  • If your application ever feels like it's getting too slow, measure where it's slow and improve those things. Don't optimize before you need to.
  • Many people these days don't call htmlspecialchars manually, but they will use a template engine instead. A template engine is much, much slower than calling this function. However, even though it's so much slower, it's still not slow enough for most people to actually care about this.
Evert
  • 93,428
  • 18
  • 118
  • 189