Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by cncguy · Jul 06, 2010 at 02:57 AM · javascriptarrayspeedfor-loop

Javascript for loops and Arrays.

For frequent access of objects from JS Arrays we assign the element of an array to a variable of defined type. When iterating through a complete array of objects is it better to use:

var arr : Array; initArrayWithObjs();

for(i=0;i<arr.length;i++) { var obj : type = arr[i]; obj.doSomething(); }

or is it better to use

for(var obj:type in arr)
    obj.doSomething();

Given a situation in which either solution is equally valid.

ie. are both as fast as the other, or not?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Eric5h5 · Jul 06, 2010 at 03:59 AM

If foreach in C# is slower, it's by such a small amount as to be insignificant. In JS, for/in is actually faster, at least in Unity 2.6. I believe there's some memory allocation involved though, which isn't present in a normal for loop, so it's possible there might be a garbage collection penalty at some point.

But in any case, it's indeed very unlikely that the choice will have any real affect on speed most code, unless you're looping zillions of times. It's usually better to benchmark yourself instead of asking what's faster...any answers you find might be either be outdated or wrong or maybe just don't apply to your particular situation, and it takes little effort to benchmark.

(As an aside, the major difference between for/in in JS and foreach in C# is that you can assign to the iteration variable in for/in but you can't in foreach.)

--Eric

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cncguy · Jul 06, 2010 at 04:25 AM 0
Share

That is interesting to note about the memory allocation. The key thing for me is that the type of the object extracted from the array is defined and not deter$$anonymous$$ed at runtime. With the standard for loop I have to allocate a variable in order to do this whereas I thought that using the 'for in' version may not allocate memory whilst still predeter$$anonymous$$ing the type.

avatar image Mike 3 · Jul 06, 2010 at 08:23 AM 0
Share

i'm not sure how the js version is implemented, but the c# version is basically syntaxical sugar for iterating through an enumerator and assigning the current value to the temporary variable, which means it's allocating slightly more than a normal for loop

avatar image
1
Best Answer

Answer by Tetrad · Jul 06, 2010 at 03:09 AM

At least in C#, the foreach is slower (which I'm assuming is pretty much the same as the for in syntax in Javascript). However the speed differences only show up when you're dealing with lists of elements of many many thousands of elements.

As with most of these "which is faster" questions, you really have to just benchmark it yourself. If you can't tell a difference, there effectively isn't a difference, and you should use the one that is the easiest to read and write.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cncguy · Jul 06, 2010 at 03:35 AM 0
Share

thanks. Will do some comparisons.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Instantiate object in for loop with array 2 Answers

which is quicker, single variables or built in arrays. 1 Answer

Turn all objects in the array to make same thing 0 Answers

(20,57): BCE0022: Cannot convert 'EnemyAI[]' to 'EnemyAI'. 1 Answer

"For"-Loop only works for final element in array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges