0
\$\begingroup\$

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?

\$\endgroup\$
  • 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 Aug 6 '13 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 Aug 6 '13 at 9:49
  • \$\begingroup\$ @JosephtheDreamer and Guffa - thank you for the suggestions \$\endgroup\$ – Nithesh Aug 6 '13 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 Aug 8 '13 at 7:33
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.