View Full Version : which is more efficient?

12-24-2003, 03:53 AM
I defined id for my cell element, now I want to refer them by id,
can someone tell which of the following way is most efficient?

1. Define same Id for a group of cells, and use document.all(id) to get a reference array of them.
2.Define unique Id for each cell element, and use document.getElementById(id) to access them.
3.Define unique Id for each cell element, and use eval(id) to manipulate them.

by the way, when I want to convert some number to float,
number=parseFloat(number) or number=new Number(number) ,
which will be executed faster?

My javascript program is running under IE6.0.

I appriciate for any suggestion, thank you.

12-24-2003, 06:57 AM
Well, #1 is completely invalid code. Even though it works in IE, it's not in the soec, and isn't even remotely cross-browser.

For #3, eval() can be slow, and is often times unreliable.

I'd say that #3 is the best choice.

As far as parsing, I'm not entirely sure which is faster, but if I had to guess, I'd say new Number().

12-24-2003, 01:30 PM
Thanks, but what is soec? and as to document.all(), if I suppose
my program will work only under IE, are there still problems with it? I need to decide if I have to revise my previous code using that function.

12-24-2003, 06:27 PM
I meant "spec", as in Specification.

And even if you're only using IE6, why not still write valid code? It would make it cross-platform anyways. I just have a problem with writing bad code when you don't have to.

Instead of using document.all.elementID, use document.getElementById(elementId). It's proper, valid, and still works perfectly fine in IE6.

document.all is unnecessarily proprietary, it's not valid DOM, and there's really no reason to use it. Besides, all you're doing is promoting Microsoft-only technology. Technology that is fairly stagnant, at least in the web world...