I am using some string which contains an html. As follows
var pagerHtml = '<span>No.Of Records</span><select class="rmselect" id="records" ><option value="10">10</option><option value="20">20</option><option value="40">40</option><option value="80">80</option><option value="100">100</option></select>';
Another is with " and escape sequence \" for " in the html as follows
var pagerHtml = "<span>No.Of Records</span><select class=\"rmselect\" id=\"records\" ><option value=\"10\">10</option><option value=\"20\">20</option><option value=\"40\">40</option><option value=\"80\">80</option><option value=\"100\">100</option></select>";
And I am writing this html to a div using jquery
Which one gives the better performance while parsing?
Is there any performance difference on parsing escape sequence?
-
2\$\begingroup\$ You should check out jsPerf. IMO, this isn't really a performance concern. I'd rather worry about maintaining this code, since HTML in JS isn't really good. \$\endgroup\$Joseph– Joseph2013年08月06日 08:46:35 +00:00Commented Aug 6, 2013 at 8:46
-
1\$\begingroup\$ Are you aware that the only difference would be when the Javascript code is parsed? When the code runs those are identical strings, so parsing them into HTML elements will give exactly the same performance. \$\endgroup\$Guffa– Guffa2013年08月06日 09:49:02 +00:00Commented Aug 6, 2013 at 9:49
-
\$\begingroup\$ @JosephtheDreamer and Guffa - thank you for the suggestions \$\endgroup\$Nithesh Narayanan– Nithesh Narayanan2013年08月06日 09:51:11 +00:00Commented Aug 6, 2013 at 9:51
-
1\$\begingroup\$ @Nithesh: Never forget that premature optimization is the root of all evil. This sort of micro-optimization really is of little use, especially with languages like JS. The JS engines change so rapidly, and the implementations can differ so much, that, when you've updated your code to work as fast as possible on V8, the engine might have changed, and your optimizations turn out to be slower on the latest version... \$\endgroup\$Elias Van Ootegem– Elias Van Ootegem2013年08月08日 07:33:46 +00:00Commented Aug 8, 2013 at 7:33
1 Answer 1
According to this jsperf test, there is no consistent difference between parsing a string literal with and without escape sequences.
There are differences depending on what browser you use, but they are quite small, and Chrome even seems to parse strings with escape sequences faster than strings without.
Explore related questions
See similar questions with these tags.