[jQuery] Re: Very slow find

2009-03-24 Thread Martin Tiršel



I need to hold some things in memory for later use, so pure html is not  
ideal. I will try JSON, if it will be under 5-7 seconds for 500 items, it  
should be ok.


Thanks

On Tue, 24 Mar 2009 06:01:33 +0100, Michael Geary m...@mg.to wrote:



If you are free to change the XML format, are you free to change it
completely?

Could you change it to HTML instead, and format the HTML in the same way
that you are doing in JavaScript now? Then you could simply use
$('#content_window_pages').load(...); and it would be very fast.

Or you could use JSON instead of XML and speed it up a lot, but HTML  
would

be by far the fastest.

Is either of those changes - to HTML or JSON - feasible? XML is the worst
choice for this kind of operation.

-Mike

--
Martin Tiršel


[jQuery] Re: Very slow find

2009-03-24 Thread Martin Tiršel



Thanks very much, this way it takes 2 seconds (with load and rendering),  
what is better I was expecting


On this project, I totally ignore IE (I am using some newer technologies  
available only in normal browsers), it is not a common website but a  
web-based application for a specific group of users.


Martin


On Tue, 24 Mar 2009 18:29:33 +0100, Michael Geary m...@mg.to wrote:




From: Martin Tiršel
I need to hold some things in memory for later use, so pure
html is not ideal. I will try JSON, if it will be under 5-7
seconds for 500 items, it should be ok.


In fact, you can get it much faster than that, even in IE.

Let's assume your JSON data is an exact mirror of the XML:

{
pages: [
{
name: the name,
id: 5,
url: http://example.com/;,
author: {
id: 7,
name: the author
},
languages: [
sk,
cz
]
},
{
// another page
},
{
// ...
}
]
}

and assume you have a reference to this data in a variable called json.

You can write:

$(#content_window_pages).append( makeTable(json.pages) );

function makeTable( pages ) {
var html = [], h = -1;
html[++h] = 'table class=pagelist';
html[++h] = 'theadtr';
html[++h] = 'thName/th';
html[++h] = 'thAuthor/th';
html[++h] = 'thLanguages/th';
html[++h] = 'thActions/th';
html[++h] = '/tr/thead';
html[++h] = 'tbody';
for( var page, i = -1;  page = pages[++i]; ) {
html[++h] = 'trtd class=first';
html[++h] = page.name;
html[++h] = '/tdtd class=second';
html[++h] = page.author.name;
html[++h] = '/tdtd class=third';
html[++h] = page.languages.join();
html[++h] = '/tdtd class=fourthnbsp;/td/tr';
}
html[++h] = '/tbody';
html[++h] = '/table';
return html.join('');
}

I think you will be very pleased with the speed of this code. And it's  
much

simpler than the XML version too. The html[++h] = business is a bit ugly,
but it's by far the fastest way to build up a long string in IE, and  
since
IE is both the most widespread browser and the slowest at this, it's the  
one

you need to worry about.

I broke up the thead section into multiple html[++h] = assignments  
just to
keep the line length short in this message, but you could combine those  
into
one as in your original code if you prefer. It's not performance  
critical so
it's just a matter of taste. For the code *inside* the inner loop I  
combined

assignments as much as possible, as in the last line of the inner loop.
-Mike


[jQuery] Very slow find

2009-03-23 Thread Martin Tiršel

Hello,

I get XML answer from server with ~5k lines and I need to parse this data  
into the table and display in HTML. I wrote a script to do this job, but  
it takes too much time to complete (~35 seconds in opera), what is not I  
want. Is there a way to improve this script? Perhaps there is more  
effective way to do this.

XML looks like:

?xml version=1.0 encoding=UTF-8 ?
xmldata
page
   nameebapylibnfwxorjwoeseksnyqumdmxssaduy/name
   id5/id
   urlojosjkfujggkildmlr/url
   author
 id7/id
 nameb/name
   /author
   languages
   langsk/lang
   langcz/lang
   /languages
/page
... 500x similar as above
/xmldata

JS:

...
 var table = 'table  
class=pagelisttheadtrthName/ththAuthor/ththLanguages/ththActions/th/tr/theadtbody';

 $(xml).find(page).each(function(i) {
   var name = $(this).find(name).text();
   var author = $(this).find(author name).text();
   var languages = new Array();
   $(this).find(languages lang).each(function() {
 languages.push($(this).text());
   });
   languages = languages.join(',');
   table += 'trtd class=first' + name + '/tdtd  
class=second' + author + '/tdtd class=third' + languages +  
'/tdtd class=fourthnbsp;/td/tr';
 });

 table += '/tbody/table';

 $(#content_window_pages).append(table);
...

XML structure can be changed if it helps to speed up the script.

Thanks for any suggestions,
Martin