Matt H
Matt H

Reputation: 235

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);

Upvotes: 0

Views: 731

Answers (2)

Stefan Sieber
Stefan Sieber

Reputation: 503

Updating JQuery will probably help.

The issue should be fixed for JQuery >= 1.12.0 / 2.2.0

Upvotes: 0

Matt H
Matt H

Reputation: 235

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.

Upvotes: 2

Related Questions