koubin
koubin

Reputation: 607

A script on this page is causing Internet Explorer to run slowly

I have jQuery code:

...
var poleVstupu= {id:['31'],platny:['1'],datum:['2013-08-05 20:23:38'], ... };

for(var poleName in poleVstupu)
{
    $('[name='+poleName+']').val(poleVstupu[poleName]).change();
}
...

in Firefox this code works fine, but in IE8 it throws ...run slowly message. If I run IE built-in debugger, after ...run slowly message it throws object doesn't support this property or method , but if I set breakpoint at $('[name='+poleName ... line, it doesn't show object doesn't ... message.

what can I do to process code faster and prevent displaying ...run slowly message?

Upvotes: 2

Views: 3677

Answers (2)

RoToRa
RoToRa

Reputation: 38390

I'm responding to the comments in this answer, because it will be longer and include code, but it most likely won't be an answer.


There is a good chance that the browser doesn't index the name attribute, so it has to loop through every single element in the document each time to find a matching name. If you limit the search to the form, you'll may have a better performance. Example:

var form = $("#form_id");

for(var poleName in poleVstupu)
{
    form.find('[name='+poleName+']').val(poleVstupu[poleName]).change();
}

A (partial) non-jQuery solution could be to access the form elements directly from DOM. Example:

var form = $("#form_id")[0]; // Get the DOM reference of the form

for(var poleName in poleVstupu)
{
    jQuery(form.elements[poleName]).val(poleVstupu[poleName]).change();
}

It may also help you'd loop over the form elements instead the object. Example:

var form = $("#form_id")[0]; // Get the DOM reference of the form

for (var i = 0, len = form.elements.length; i < len; i++)
{
    var element = form.elements[i];
    if (typeof poleVstupu[element.name] !== "undefined")
      jQuery(element).val(poleVstupu[element.name]).change();
}

The performance of jQuery increases with every version. Can't you use 1.10.x or even 2.x?


How is the performance if you leave out .change()?


EDIT:

On non form elements name is invalid anyway, so you shouldn't be using that. I'd use the id and set a class on all elements where you need to do this:

$(".change-html").each(function() {
  if (typeof poleVstupu[this.id] !== "undefined") {
    $(this).html(poleVstupu[element.name]);
  }
});

Or if you can't use the id, for eaxmple, because you have duplicates, use a data- attribute.

<p data-change-html="your_name"></p>

$("[data-change-html]").each(function() {
  var element = $(this);
  var name = element.data("change-html");
  if (typeof poleVstupu[name] !== "undefined") {
    element.html(poleVstupu[name]);
  }
});

(But the latter won't be very fast mostlikey).

Upvotes: 2

Bryce
Bryce

Reputation: 6610

Are you running your IE inside a virtual machine? If so, make sure you give the virtual machine enough RAM allocation and CPU power (if settable) so scripts can run using a significant portion of your computer's abilities.

Upvotes: 0

Related Questions