Hi,

thanks fort his little optimization tutorial :-)

One question, is there a difference between a loop with a running
variable (e.g. for (i=0; i<rows.length; i++) ) and a for-in loop (e.g.
for (var i in rows) )?

I've done some tests and couldn't find any performance difference
beside the normal variation between two different measurements.
Which variant should be preferred?

by(e)
Stephan


2009/2/6 jQuery Lover <ilovejqu...@gmail.com>:
>
> That is true....
>
> I always suggest using built in for() loop with huge data sets... and
> people keep ignoring me :)))
>
> ----
> Read jQuery HowTo Resource  -  http://jquery-howto.blogspot.com
>
>
>
> On Fri, Feb 6, 2009 at 8:25 AM, Michael Geary <m...@mg.to> wrote:
>>
>> "...there is not much room for improvement left."
>>
>> You just know that when you say that, someone will come along with a 20x-40x
>> improvement. ;-)
>>
>> http://mg.to/test/loop1.html
>>
>> http://mg.to/test/loop2.html
>>
>> Try them in IE, where the performance is the worst and matters the most.
>>
>> On my test machine, the first one runs about 6.3 seconds and the second one
>> about 0.13 seconds.
>>
>> -Mike
>>
>>> From: Ricardo Tomasi
>>>
>>> Concatenating into a string is already much faster than
>>> appending in each loop, there is not much room for
>>> improvement left. What you can do improve user experience
>>> though is split that into a recursive function over a
>>> setTimeout, so that the browser doesn't freeze and you can
>>> display a nice loading animation.
>>>
>>>
>>> On Feb 5, 5:03 pm, James <james.gp....@gmail.com> wrote:
>>> > I need tips on optimizing a large DOM insert to lessen the
>>> "freeze" on
>>> > the browser.
>>> >
>>> > Scenario:
>>> > I receive a large amount of JSON 'data' through AJAX from a
>>> database
>>> > (sorted the way I want viewed), and loop through them to
>>> add to a JS
>>> > string, and insert that chunk of string into a tbody of a
>>> table. Then,
>>> > I run a plug-in that formats the table (with pagination, etc.).
>>> > Simplified sample code:
>>> >
>>> > var html = '';
>>> > $.each(data, function(i, row) {
>>> >      html += '<tr><td>data from json</td></tr>';});
>>> >
>>> > $("tbody").append(html);
>>> > $("table").formatTable();
>>> >
>>> > formatTable() requires that the table has to be "completed"
>>> before it
>>> > can be executed.
>>> > Is there any way I can optimize this better? I think I've read
>>> > somewhere that making a string too long is not good, but I've also
>>> > read that updating the DOM on each iteration is even worst.
>>> >
>>> > Any advice would be appreciated!
>>>
>>
>>
>

Reply via email to