Matt H Matt H - 5 months ago 17
Javascript Question

Why is RegExp.test consuming a lot of time in IE?

When using jQuery to load HTML into an element, I am seeing very poor performance in IE. Chrome is not showing any performance problems. Changing the IE browser mode does not appear to improve the performance.

When running the profiler in IE, I see that most of the time is being spent in the

RegExp.test
function.

HTML:

<div id="content"></div>



JavaScript:

var htmlString = <div>...Lots and lots of content...</div>

$('#content').html(htmlString);



  • jQuery version: 1.9.1

  • IE version: 9


Answer

When loading a large amount of content, don’t start with an opening tag and end with the closing tag.

Change content to: var htmlString = <div></div><div>...Lots and lots of content...</div>

After researching the problem I found this jQuery bug request that pointed me in the right direction. jQuery Ticket #11456

It appears that for IE compatibility, JavaScript RegExp is used to parse the content that is being loaded. When the content starts with a tag, it tries to find the end tag (or something like that). If the amount of data is large, it can take quite a while to do this parsing. I solved my problem by putting an empty at the beginning.