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++)
		Document.images[i].src=”blank.gif”l
</script>

<Script language =”JavaScript”>
var theimages=document.images;
for (i=0;i<theimages.length;i++)
	theimages[i].src=”blank.gif”
</script>

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

TECH_NETAsked:
Who is Participating?
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.
0
StealthyDevCommented:
a. both are equally efficient.
0
rdivilbissCommented:
And they both have invalid script opening tags as of 1999.  Is this your homework?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TECH_NETAuthor Commented:
rdivilbiss:

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?

0
rdivilbissCommented:
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 http://www.javascriptkit.com/javatutors/efficientjs.shtml (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++) {
            document.images[i].src="blank.gif";
      }      
</script>



<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++) {
            theimages[i].src="blank.gif";
      }      
</script>

Both are more efficient than your examples.
0

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
0
rdivilbissCommented:
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.)

Regards,
Rod
0
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.
0
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
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.