I know that using single quotes around a string in PHP is faster than using the double quotes because PHP doesn't need to check for variable presence in the single quoted string. My question is which will perform better:
A) A double quoted string with variables present:
echo "foo bar $baz";
or
B) Single quoted with a concatenated variable:
echo 'foo bar ' . $baz;
# this code prints the output within quotes. The choice between both the types (single quotes and double quotes) depends on the programmer's choice. Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL.
If no processing of the text within is required then single is faster.
Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!
A few repositories of popular JavaScript projects reveals that single quotes are favored over double quotes. You can see that front-end libraries (React, Angualar) have more double quotes than the other libraries as might have to do with the presence of HTML fragments.
I did a benchmark of this on a blog I was working on a while ago. However, as I've come to realize there are a lot of variables. Chief among them are:
Over all I'd say it really isn't that big of a deal for you to actually worry about it. Generally speaking it's only going to make a noticeable difference if you're writing a huge site (think MySpace, Facebook, Flickr, etc) and usually by that point you have so much hardware behind you that the single vs double quotes thing becomes irrelevant again.
Personally I'd say there's far more important things that will impact performance in a much more substantial way (caching, sql optimization, auto loading to prevent unnecessary includes, etc).
I personally choose single quotes nearly every time, but not for speed. I do because I think it's more readable. And that to me is important.
In all honesty, I don't think you need to be scrutinizing this particular convention when looking to save performance. But, for what it's worth, I think I recall reading that variables within strings are less-efficient (mind you, it's negligible and in my opinion not an issue) than concatenation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With