JavaScript vs C# in Unity

Hi All,

I’m a guru with both C# and JavaScript, but I think I like JavaScript more just for this sort of coding. The question is, typically performance wise C# beats JavaScript hands down. This platform however from my perspective looks as though it is interpreting the script files and compiling them down to something else entirely.

So my question is, has anybody done any bench mark performance tests to see what the performance difference is for JavaScript vs C# here?

How does Unity behind the scenes deal with the scripts? What are the primary difference (syntax aside) for the two language choices with this platform? Are there known bugs for either?

Thanks,
~David

This question has been answered at least 50 times. Just ask auntie google.

I can’t answer all of your questions but… JavaScript in Unity is not pure JavaScript. It’s not the same as what you use in your browser. The Unity compiler will compile JavaScript files just as it does C# files. Performance wise, I don’t think there’s any difference as they end up with essentially the same result.

That being said, it mostly depends on your comfort level with a particular language. If you are a C# guru though, not sure why you’d ever choose JavaScript. :slight_smile: All of that aside, there are more tutorials and examples in C# I believe than there are in JavaScript so it may be a better approach for you.

Use C#.

Fewer limitations.
Harder to write bad (performance/memory) code.
Less Unity-specific.

There are differences in speed, which C# generally wins. I don’t have link to the thread with all the data in it, but there was a bunch. It’s mostly once you get to a few million iterations, you get c# going a bit faster.

So I found a few posts, but not necessarily pointing to performance. If it really is the millionth iteration before performance difference is noticed, I think i’m going opt for JavaScript in this scenario. Nothing can beat the flexibility of being able to be like: var foo = 5; foo = function() { dostuff(); return 4;}; Or just generally alter behavior on the fly like that. much more complex to do the same in c#.

Unity’s “JavaScript” is nothing like web JavaScript so some of the things you just mentioned either aren’t possible at all or won’t work on certain platforms.

It’s not just the millionth iteration. In my previous game, which was all written in UnityScript, I had to use perlin noise from one of Unity’s custom example scripts (it was before Unity put it in the source). That script was in C# from the example, and initially I used it as it was, and it was running fine.

At some point though, I needed to access my Perlin Noise on runtime, and the compilation order from C# to UnityScript was giving me headache, so I sat down and translated word for word the script from C# to UnityScript. The conversion was proper, everything strictly typed, no downcasts, nothing that would impact performance. Still, just from the translation, I lost about 2-3 fps for every particle system using my translated Perlin algorithm. The particles were about 500 per system, so hardly a million iterations.

UnityScript’s impact is more apparent on mobile devices. On PC you actually might need a million iterations or more to “feel” any sort of impact.

Oh god… Why this question again…

Perhaps because the engine goes through iterations and language dynamics from 3 years ago might not be the same today. It doesn’t hurt to ask or give opinions.

No idea what you’re talking about…

var foo = 5;

…is completely legitimate code in C#, tho I have no idea why you’d want to do that.

Also, I hardly think this is any harder than your anonymous function example…

Func<int> foo = ()=> { DoStuff(); return 4; };

or if you prefer a keyword to identify the method…

Func<int> foo = delegate() { DoStuff(); return 4; }

I would say C# is the wiser choice simply because its a real language, rather than some form of script thats not usuable outside of Unity.

Youll also find a lot more tutorials for c#, even if they arent Unity related, where as you wont get the same thing with UnityScript since its a Unity specific language which is loosely based off js.

I always hear people say that UnityScript is unusable outside of unity, why? Unity compiles UnityScript files to .net DLLs that should be able to plug right in to a Visual Studio project. How would Visual Studio know what language was used to create the DLL? Maybe I’m missing something but theoretically it seems like UnityScript should be just as viable outside of unity as C#.

The implication being that you’re still relying on Unity to do the initial compilation. You couldn’t write UnityScript in Visual Studio and compile it to a DLL.