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
24
Question by Ricardo · Oct 20, 2009 at 01:37 PM · performanceoptimizationjavascript-specificlanguage-comparison

Is there a performance difference between Unity's Javascript and C#?

Is there a significant performance difference between writing code in Unity's Javascript and C#? If so, what are the pitfalls a Javascript developer should avoid?


See also: How should I decide if I should use C#, JavaScript (UnityScript) or Boo for my project?

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

6 Replies

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

Answer by Lucas Meijer 1 · Nov 16, 2009 at 11:55 PM

After doing some recent profiling, I've actually found some cases where unityscript does some stuff behind the scenes, that would be slower than if you had written in c#. You can defenitely write UnityScript code that is as fast as the C# alternative, however, it is easier to accidentilly write slow code, because UnityScript tries to be helpful, and does some stuff behind your back to make what you want to do actually work.

Comment
Add comment · Show 6 · 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 Ricardo · Nov 17, 2009 at 12:08 AM 18
Share

Hi Lucas, can you comment on the areas with more overhead so that people can be aware of them?

avatar image SisterKy · Jan 08, 2010 at 04:42 PM 2
Share

I'd like to second Ricardo's request...

avatar image KvanteTore · May 03, 2010 at 09:44 PM 0
Share

@Sister$$anonymous$$y: then why don't you upvode Ricardo's comment? :)

avatar image Waz · Apr 20, 2011 at 10:22 PM 4
Share

With #pragma strict, you're pretty safe from this "helpfulness".

avatar image Bunny83 · Aug 02, 2014 at 01:16 AM 2
Share

@sidhi: It is still the same as Unity doesn't use JavaScript but UnityScript which is a .NET / $$anonymous$$ono language and has nothing to do with web Javascript beside the syntax which is inspired by Javascript. All 3 languages (C#, UnityScript and Boo) are compiled into the same IL byte code in the end. However as Lucas mentioned there are cases where UnityScript has some overhead as Unity does things behind the scenes.

One of the most common things UnityScript does for you is wrapping access to members of built-in struct-properties like .position / . rotation of the Transform component. The color property of components which have such a property. And some others.

UnityScript allows you to do this:

 transform.position.x = 5;

This however is converted to:

 var tmp = transform.position;
 tmp.x = 5;
 transform.position = tmp;

This is necessary since position is a property and Vector3 (the type of that property) is a struct (a value type). To actually change the position the setter of the property has to be called with a new position.

In C# when you do transform.position.x = 5; You only call the getter of the property (which returns a copy of the position vector as it's a value type). It's pointless to change the x member of that copy as the copy isn't assigned back to the setter. In C# you are forced to use the second example above but UnityScript "does this for you".

If you do something like this in UnityScript:

 transform.position.x = 5;
 transform.position.y = 0;
 transform.position.z = 0;

it will become this:

 var tmp1 = transform.position;
 tmp1.x = 5;
 transform.position = tmp1;
 var tmp2 = transform.position;
 tmp2.y = 0;
 transform.position = tmp2;
 var tmp3 = transform.position;
 tmp3.z = 0;
 transform.position = tmp3;
Show more comments
avatar image
14
Best Answer

Answer by burnumd · Oct 20, 2009 at 02:53 PM

In general, no, but utilizing the dynamic typing feature in the Javascript style scripting can lead to slower code than you might anticipate. If, for example, you have a variable that at one point contains an int and then use that same variable to hold a float, you are, in essence, using two separate variables. The pitfall here is that while writing, you may look at it and think "Ah, got that done with one variable, nice!" when you haven't really saved anything at execution time. To that end, I'd recommend using "#pragma strict" as much as possible when writing Javascript code. Putting that line at the top of any Javascript script forces static typing (and is required for using Javascript flavored scripting on iPhone).

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 MountDoomTeam · Sep 30, 2012 at 06:39 PM 1
Share

i just had instantiation of objects taking one $$anonymous$$ute for 1000 objects when normally it's just 10 seconds so i put on strict and it has errors. EDIT- i had not declared the 3 variables of

  function AddChildren( iter:GameObject, ratio:float , depth:float)

it was just

 function AddChildren( iter, ratio , depth)

so your optimisation makes it go from 1 $$anonymous$$uts to 8 seconds.

nice one

avatar image
13

Answer by Michael La Voie · Nov 16, 2009 at 07:55 PM

@burnumd and @Jormungandr are correct in that JS should run at roughly the same speed as C#; however, there is a larger issue: For windows developers, using Visual Studio to write your code offers huge advantages and it has substantially better support for C# than for JS.

Visual Studio 2008 is free (the express edition) and offers:

  • Code completion
  • Error highlighting as you type
  • Allows you to comment your methods so that the comments will appear as floating help bubbles when you use them.
  • For C#, a large and excellent library of supporting function
  • There will also be a much larger community that can answer C# programming questions than can answer JS questions (most JS forums will focus on JS in the context of HTML, while C# is used for every purpose imaginable even, to some extent, game development)

Two things to consider for the future

1) When VS 2010 comes out, the JS support looks like it will improve dramatically so that may mitigate these arguments; however, C# is Microsoft's baby so no matter how much work they put into JS, they will likely put more into C#.

2) The Unity team has promised to add better Visual Studio integration in the future, recognizing how beloved VS is amongst windows developers. (MS may or may not be evil and their products may or may not be crummy, but VS is one of their greatest accomplishments)

Comment
Add comment · Show 3 · 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 Ricardo · Nov 16, 2009 at 08:36 PM 0
Share

That's also leaving aside that Unity's JS is not quite Javascript, but I'm sure they could write a plug-in to support it.

avatar image Michael La Voie · Nov 16, 2009 at 09:26 PM 0
Share

Good point. Full disclosure, I'm a professional C# developer; however, I'd consider using JS/UnityScript if they created an addon for VS to fully support the language. I've been very impressed with the Unity $$anonymous$$m. I'm not sure this is the best use of time, but I have no double they could do it if they wanted to.

avatar image Yves · Jul 26, 2010 at 08:37 AM 0
Share

On this topic of IDE and language choices, One could consider NetBeans IDE for JavaScript as it is fully integrated and supported by a very large community as well.
Going a bit further, we could even wonder about unityscript being integrated under NetBeans as are python, php, etc.. Wtih the current editor one cannot evaluate if they are other options of parameters or the parameter structure of a function or class. Yves

avatar image
5

Answer by Jormungandr · Oct 20, 2009 at 02:05 PM

Generally, no, there is no performance difference. Javascript and C# both compile down to the same CLI bytecode, and that is what's actually executed by the Mono runtime.

Comment
Add comment · 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
2

Answer by Gunder · Dec 10, 2009 at 02:27 PM

im afraid javascript is slower because it is a dynamic language.

Look at the byte code generated, and its disassembled from the orginal:

C#

this.guiElement.color.a=1;

And the C# generated from JS:

object obj = RuntimeServices.GetProperty(this.guiElement, "color");

RuntimeServices.SetProperty(obj, "a", 1);

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 MountDoomTeam · Sep 30, 2012 at 06:42 PM 0
Share

nice one. it's tricky to benchmark it but it'spossible.

.. but why generate c from js, i though that it went straight to booscript?

avatar image FuzzyQuills · May 30, 2015 at 02:23 AM 1
Share

This may be old, but... Had no idea that JavaScript does that. Where did you source it from?

  • 1
  • 2
  • ›

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

Memory management and C# do's and don'ts? 5 Answers

What are the Syntax Differences in C# and Javascript? 7 Answers

Question regarding this line of code 1 Answer

Script translation 2 Answers

How should I decide if I should use C#, JavaScript (UnityScript) or Boo for my project? 14 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