Question in code efficiency in javascript

Which of the following code snippet s is more efficient. see the code section for the snippets

<Script language=”JavaScript”>
	For (i=0;i<document.images.length;i++)

<Script language =”JavaScript”>
var theimages=document.images;
for (i=0;i<theimages.length;i++)

a.	Both are equally efficient
b.	The first code is more efficient as it contains less code
c.	The first code is more efficient as it employs objet caching
d.	The second code is more efficient as it employs object caching.

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
2nd one is more efficient ( d is the answer).

You dont have to do "document.images" every time, there by reducing the memory as well as time cost.
a. both are equally efficient.
And they both have invalid script opening tags as of 1999.  Is this your homework?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

TECH_NETAuthor Commented:

The invalid script is my typo mistake. This was one of the question i had received in my certification test and i answered 'd'. and wanted to make sure that was the correct answer.

I could memorize a few of the questions which i felt i had some doubt about?

Honestly i do not know how to accept a solution although i am inclined to d. senthurpandian: mentions that both are efficient. Can you tell me why?

Let n = document.images.length then

i<document.images.length;  // <- this calls document.images nx2 times;

var theimages=document.images;   // <- this calls document.images 1 time
                                                         //     but also calls theImages nx2 times.

According to (which is the exact same example) the second is more efficient because document.images is "cached" in theImages.  I'm not sure I agree, but it may be true. I don't know the cost of accessing either in machine cycles so I can't say.  The author of the web page says the second is more efficient.

But, as written, neither will execute.  Now...

<script type="text/javascript">
      var cnt = document.images.length;
        // calls document.images n+1 times
      for (i=0; i<cnt; i++) {

<script type="text/javascript">
      var theimages=document.images;
      var cnt = theimages.length;
        // calls document.images 2 times, but calls theImages n+1 times
        // so maybe my first example is more efficient.
      for (i=0; i<cnt; i++) {

Both are more efficient than your examples.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TECH_NETAuthor Commented:
Since i am not the expert and there is no way i can verify which is a better response. i leave it to the moderator to share the points across all the participants. Thanks everyone
No offense but the moderators have many responsibilities and that does not normally involve closing questions. That's the clean up crew who are entirely overworked. Closing a Q and assigning points are your responsibility.

If you can't decide which code is more efficient you could write a test page that loads a hundred+ images and see which code works better. (They can all be the same image as that doesn't matter to the example code.)

TECH_NETAuthor Commented:
Thank you for the suggestion. I will try it out as suggested and response back.

I just want to ensure that i do not offend anyone with my marks split.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.