- Home /
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?
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.
Hi Lucas, can you comment on the areas with more overhead so that people can be aware of them?
@Sister$$anonymous$$y: then why don't you upvode Ricardo's comment? :)
With #pragma strict, you're pretty safe from this "helpfulness".
@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;
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).
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
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)
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.
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.
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
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.
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);
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?
This may be old, but... Had no idea that JavaScript does that. Where did you source it from?