views:

141

answers:

2

I have a project using Javascript parse json string and put data into div content.

In this case, all of itemname variables is the same div's id.

If variable i about 900, I run this code in Firefox 3 for <10ms, but it run on IE 7 for >9s, IE process this code slower 100 times than Firefox

I don't know what happen with IE ?

If I remove the line document.getElementById(itemname), speed of them seems the same.

The main problem arcording to me is document.getElementById() function?

Could you show me how to solve this prolem to increase this code on IE ?

Thank in advance.

            var i = obj.items.length-2;
            hnxmessageid = obj.items[i+1].v;

            do{
                itemname = obj.items[i].n;
                itemvalue = obj.items[i].v;
                document.getElementByid(itemname);
                i--;
            }while(i>=0);
+1  A: 

Are you really noticing any latency?

gEBI is natively very very fast, I don't think you can avoid it anyway for what you're doing. Could you provide a low-down of what you're doing precisely? It looks like you're using a loop, but can you post exactly what you're doing inside of the loop, what your common goal of the script is?

meder
I use this code to render data from server to client.In fact, the line document.getElementByid(itemname) will be in real:document.getElementByid(itemname).innerHTML(itemvalue);I get json from server through XHR request, then convert it to array obj as you see I want to post simple code for you to analys easier!have you other ideas for this problem ?
misamap
Why not send HTML from the server and inject it all in a single element. So move some of the presentation logic on the server.
Ionuț G. Stan
Yeah looks like the overhead is definitely not `gEBI` but instead the parsing of the JSON, I recommend you use a different technique for the parsing part, or do what Ionut suggested and send more content in one http request instead of several.
meder
Thank your advise! But I want to find the solution for getElementId() function, because I have to call it more and more later, but in this case, it shows very slow on IE, how can I do?
misamap
um - the direct `document.getElementById` is the absolute fastest way that I know of, of accessing the element by its id.
meder
@ Ionut G. Stan: For details, this project relate to stockquotes, it's a live priceboard, so I want to fill value into cell of the table, so I use this technique, because data push from server continuously
misamap
@meder: have you know tips or tricks to increase performance this on IE, such as using setTimeout or tweak IE or etc...?
misamap
@misamap - the only way you're likely to get a performance boost is not by replacing gEBI but refactoring the code that does the JSON parsing. Please paste more code.
meder
@meder: I optimzed code parse JSON, but big prolem is getElementById() function, it not involve parse JSON. I have tested parse JSON on Firefox and IE, result the same speed. I know problem in this case is getElementById() but I don't have any solutions about this. It so nervous with me! I have tried someday but it failed!
misamap
Are you sure you've optimized it to the fullest extent? Since you don't want to paste anything the only thing I can mention is that you might be able to rely on `document.foo` dom 0 syntax where 'foo' is the name="value" of the element, this is faster because there's no looping involved but it's a very very bad practice. I still think you need to refactor the other code.
meder
This is cache technique! But everything againts me! Nochange for this case. I think JS engine of IE have bug or complex logic when call getElementById function, I think you can help me find the right solutions in this
misamap
@misamap, so let my understand. You execute that loop for every response from the server? And in that loop is gEBI? Then move those calls to gEBI outside the loop. Use a closure to the references of that elements. This way gEBI will only be called just at page load, not after every server response.
Ionuț G. Stan
@Ionut G. Stan: i use XHR request, it get data background and I use above code to render data. On page load, I don't execute this code, it's only run after page load.
misamap
A: 

document.getElementByid(itemname) is the fastest way to get a single element from the DOM in any real application you will sould not see any problems with using it, if you do see a problem you need to rethink you code a little it possible to acomplish any task with just a handfull of calls for this method. You can present you full problem if you like so I could show you an example

MichaelT
for more details, I can explain it look like the table, my mission is get stock data from server then render it into table.Table contains about 26 columns and 300 rows, row total can expand in the future. Data request continuously, aproximately 300ms each time, so javascript engine must process code very fast, the code above simply to process get address of cell to render data, still many tasks complex I can't post here, but in the code which I give, in IE, it consume 9 second, very slow for me, remember Firefox process sam this code do 10ms
misamap
my questions give in this case, IE really accept this problem? You can test with yourself table
misamap