Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

does loops make a difference?

loop

  • Please log in to reply
10 replies to this topic

#1 even821

even821

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 132 posts

Posted 05 November 2012 - 09:58 AM

is there any difference in runtime for javascript code if i were to write for instance 100 variables manually as opposed to using a loop to write them? i mean other than the obvious time it would take to actually write them (in wich case the loop would probably be the best)

im asuming none of them are running faster or slower than the other? since they essentially do the same thing, or am i wrong?
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 05 November 2012 - 10:32 AM

Why would you want to manually code 100 variables rather than use a loop? Seems to me any performance difference (if any) would be infinitesimally small compared to the absolute uselessness of 100 hard-coded variables compared with, say, an array of 100 spaces.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 05 November 2012 - 10:38 AM

100 variables, no difference...
10 000 variables, you might start to see a difference, but it's still gonna be pretty small
So insted of typing 100 or 10 000 variables manually, take that time and check your code elsewhere to look for better optimisation.
  • 0

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#4 Colanth

Colanth

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 165 posts
  • Location:Fayetteville, NC, USA
  • Programming Language:PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Pascal, Logo, Assembly, VBScript, Others

Posted 05 November 2012 - 11:34 AM

Technically, inline code is always faster than calls or looping. (The loop statement takes a finite amount of time to execute, and it has to execute once for every loop.)

Considering that many people are still running browsers with crappy Javascript interpreters (like IE6), the difference in execution time on a given computer will probably be lost in the noise. (Even IE9 can bog down with certain Javascript constructs.) If the data is such that it can be written in a loop (data1, data2, data3, etc.), do it in a loop.
  • 0

#5 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 05 November 2012 - 04:00 PM

interesting ... if you work for yourself then do a loop ... if you work for a company then do 100 variables ... since you will get more pay :) ... just some thing to cheer up ... I guess you already knew the answer :)
  • 1

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#6 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 05 November 2012 - 04:19 PM

interesting ... if you work for yourself then do a loop ... if you work for a company then do 100 variables ... since you will get more pay :) ... just some thing to cheer up ... I guess you already knew the answer :)

That is at the exact same time, the best and the worst answer :P
But you forgot to mention to not use copy/paste but to type each letter
  • 0

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#7 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 05 November 2012 - 09:19 PM

Technically, inline code is always faster than calls or looping. (The loop statement takes a finite amount of time to execute, and it has to execute once for every loop.)


There you got some answer, i add up that since javascript is an interpreted code, you will just have to decide which time you want to expend: parsing time (when file loading) or execution time. the diff is you can use the execution time in a iframe or after document.onload, parsing is always at first and will make the page rendering slower.
when still will look like no difference in most browsers, it might have some loading impact in no-coders, old machines, these with low hardware and full of **, you have no idea how bad an old explorer to the neck with crappy plugins looks like, lol!
  • 0

#8 even821

even821

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 132 posts

Posted 05 November 2012 - 11:10 PM

haha thanks for the replies guys :)
as a programmer i would probably always use loops, since it saves time while writing the code. its just something i was rather curious about, and thus felt i needed to get an answer :)
sometimes its just fun to learn the small, more insignificant stuff about code and not just the bigger more appealing stuff. thanks again.
  • 0

#9 BenW

BenW

    CC Devotee

  • Community Manager
  • 976 posts
  • Programming Language:ActionScript
  • Learning:ActionScript

Posted 06 November 2012 - 04:58 AM

That is at the exact same time, the best and the worst answer :P
But you forgot to mention to not use copy/paste but to type each letter

Now, who wants to test it (including typing each variable out themselves)? :laugh:
  • 0

fss_overall.png

 


#10 Colanth

Colanth

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 165 posts
  • Location:Fayetteville, NC, USA
  • Programming Language:PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Pascal, Logo, Assembly, VBScript, Others

Posted 06 November 2012 - 02:55 PM

It's trivial to write a program to write a JS routine with 100,000 variables. The fun comes in when you run the web page into IE5 or 6 and grow old hitting the "continue" button 10,000 times.
  • 0

#11 preetisoft2

preetisoft2

    CC Regular

  • Member
  • PipPipPip
  • 26 posts
  • Programming Language:C, Java, C++
  • Learning:PHP

Posted 09 November 2012 - 02:14 AM

There is no difference but will be better to use loop for a large number of executions.
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download