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?

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.

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).

@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)

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.

there is a lot of stuff about this in forums in a topic about scripting languages. there is another thing that some guys are talking about on the net that the CIL bytecode compiler of javascript is not as good as C#'s. mono developers put much more on C# compared to javascript.

also i want to say unity script is javascript. it's different from browser's DOM javascript but DOM is not about language and this language is javascript. it's version is not the newest one and things like properties are not supported i think.

i want to motivate all of you to use C# for many reasons stated in unity forums about choosing a scripting language

keep in mind that the most important aspect of your code is the algorithm and doing stuff in C# or javascript is not as important as that.

P.S C# 4 has dynamic features :(

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

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



And the C# generated from JS:

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

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