Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by em2 · Aug 25, 2011 at 05:18 AM · javascriptswitchifefficiency

JS switch vs multiple if's efficiency?

Hi All, Is there any difference in the efficiency of code execution when using multiple if statements as opposed to making a single switch statement?

It's on an OnTriggerEnter(hit : Collider) function to try and capture the multiple collision possibilities for a projectile. Thank you.

Cheers Chris

Comment
Add comment · Show 4
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 zannghast · Aug 25, 2011 at 05:35 AM 0
Share

I can't really answer as I can't explain in detail what I'm about to say but..

It depends on the nature of the logic you want to implement. If it's consistently going to be traversed over the duration of your game (and if there's really a LOT of conditions there..) you may want to try doing a switch statement. Switch statements have extra optimization that is done in the background when compared to if-else blocks.

avatar image zannghast · Aug 25, 2011 at 05:52 AM 0
Share

Here's a study that compares the execution time of both an if-else block and a switch statement. You'll notice that it would generally take a LOT of iterations for the difference to be even obvious. http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx

avatar image em2 · Aug 25, 2011 at 06:02 AM 0
Share

Thanks Dendro, that looks like what I was after. All the best Chris p.s. if you like put it up as an answer and I'll mark as correct. Thanks again.

avatar image em2 · Aug 25, 2011 at 07:21 AM 1
Share

Dendro's answer in comments above was considered the right answer. Chris

1 Reply

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

Answer by Joshua · Aug 25, 2011 at 08:14 AM

Switch statements are indeed more efficient, but "Switch statements have extra optimization that is done in the background when compared to if-else blocks." isn't really the reason.

The reason is that the expression is evaluated once and then the side effects get executed. With several if else if else statements each if gets checked one by one until one returns true and gets executed.

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 em2 · Aug 25, 2011 at 02:23 PM 0
Share

thanks joshua. As opposed to nested if statements i was actually referring to multiple individual if's rather than nesting to else and else if's. I should have been more specific in fra$$anonymous$$g the question. The article link gives a good speed comparison though. Thanks again for responding. Best regards Chris.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is a Switch Categorized by If statements more efficient than Large Switch Statement? 1 Answer

Adding an object to an array of custom objects (JS) 1 Answer

Checking Player's distance to an Object 2 Answers

Camera switch after specific time 1 Answer

If statement not working 3 Answers


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