Related to the answer https://stackoverflow.com/a/10619477/1076753 to cleate an element is better to use
$("<div>", {id: "foo", class: "a"});
or
$("<div />", {id: "foo", class: "a"});
They both works, but which is better or correct to use?
The official API Documentation says that to ensure cross-platform compatibility, the snippet must be well-formed. Tags that can contain other elements should be paired with a closing tag:
$( "<a href='http://jquery.com'></a>" );
while Tags that cannot contain elements may be quick-closed or not:
$( "<img>" );
$( "<input>" );
So, at the end it's wrong to use one of the first two options for a div?
taking a look into the jQuery library, the following is the relevant section from v2.2.0 line 2827.
You will see that it checks if the selector is a
string
, and if it does then see's if it starts with<
and ends with>
.Then, having in mind the regex
rsingleTag
being:-Which matches both
"<div>"
and"<div />"
, returningdiv
asgroup[1]
.The
parseHTML
uses that to return adiv
element, in themerge
:-then using the regex again, and
context
as your object for setting the properties:-it
for
's over each property setting withthis.attr(match, context[match]);
As above shows, its personal preference. Both work the same.
If you go solely by metrics that can be easily measured: The first is better because it is 2 bytes shorter.
Once you start accounting for readability and other less tangible considerations, it becomes largely a matter of opinion.
No, that section only applies "If the HTML is more complex than a single tag without attributes"
From jQuerys website:
Notice that it doesn't mention HTML generation.
JavaScript provides the
document.createElement
method. jQuery utilizes this method if HTML-like string is passed as the selector.The jQuery entry point (
$(whatEverIFeelLikeDoingToday)
) type checks the selector for string, node or function, then handles the request accordingly. If the argument is string, it then passes through +/- 60 lines of code (Am I HTML? Am I a selector, am I an ID, am I a class?). After the HTML-like string is identified, it is then passed to another function (additional function calls) to be generated -therefore slow (see benchmark below). In fact, it doesn't add much value to the process, aside from uniformity, just slows it down.I've found the best practice to be: not using jQuery for rather simple tasks (i.e. node creation) -wherever practice, eliminate the obstacles in it's way.
Notice the significant performance increase in this benchmark. (Just about 4x faster in Chrome). This solution is Faster than the two described above and inherently Cross platform.
I don't think that the argument for less bytes -vs faster execution time is in the scope of this thread. However, assuming you are minifying your code, I will throw out a the practical example of looping through an array to generate html rows for a table. Bytes aren't the problem, you only needed to load the script once -execution is what bogs you down.
With better or correct use in your case it depends on how frequently this code will appear in your page. @Quentin is correct in that there is only a two byte difference between the first and second option. Now if the code only appears a few times in the whole page then realistically unless you are recording a large amount of traffic on your server you will not see a significant difference between the two, on the other hand if you are running a medium to high traffic site or will be having a large number of instances of this javascript appearing on your page then the best option would be to go with option one as it is two bytes smaller and over a larger page with many instances of this code you will find measurable savings in the byte size of the page.
As for well formed tags for cross platform compatibility both the first and second options will result in the exact same end result in the browser so in your use case compatibility will not be affected by either option.
Based on these points I would suggest using option one as it provides the two byte advantage over option two, that is effectively the only difference between the two options in your use case.