Why is C# a lot faster than Javascript and Java?

peq42_

Yeet
Veteran
Joined
Feb 5, 2016
Messages
543
Reaction score
327
First Language
Portuguese(BR)
Primarily Uses
RMMV
(I know it's not exactly talking about javascript only, but since the answer will tell how javascript runs in the system, i thought it MAYBE was the right place to post)

Since I started learning about MV, javascript, NwJs, NodeJS and stuff, I also started learning more about how programming languages work, their focus,etc. Few weeks ago i found the website "The Computer LanguageBenchmarks Game" and it's really interesting:It runs similar tasks using different languages in order to show their speed, then it compares one to another that's similar. To me it's something really useful, since this website is updated every few days/weeks, what can show the progress of some languages, at least when it comes to performance.

Getting to the point: randomly I entered the "C#" page and noticed that it's faster than java in most things, which makes it a LOT faster than javascript(which loses to java in performance on everything).

How can it be? I mean, all of them are interpreted programming languages(wikipedia), They have "Just in time compilers"(At least Java and javascript have[Chrome and Firefox], C# I don't know much),etc.
What makes C# That much faster than Javascript, which nowadays runs over an engine compiled in C++ and made by huge enterprises like Google, Mozilla,etc ? And what about Java, that's older than C#, a lot more used and which receives many more updates?
 

RocketKnight

Broke with expansive taste
Veteran
Joined
Apr 9, 2012
Messages
508
Reaction score
868
First Language
Portuguese
Primarily Uses
RMVXA
C# is more direct to the hardware also it's pre compilate.
 

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
857
Reaction score
991
First Language
Finnish
Primarily Uses
RMMZ
Java and C# must be compiled (even if it's JIT) and as far as I know Javascript can't be compiled it's "interpreted" by JS engine and that's why it is slower and has extra layer(s) before machine language.

Edit: Ok ok... Anything can be compiled if there's compiler for it, but at least I havn't seen compiler for JS :)
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
4,969
Reaction score
4,554
First Language
English
Primarily Uses
RMMZ
It's basically because of the way the runtime is structured; the way C# works under the hood is a lot more streamlined and efficient than Java is, especially in terms of things like structs.
 

peq42_

Yeet
Veteran
Joined
Feb 5, 2016
Messages
543
Reaction score
327
First Language
Portuguese(BR)
Primarily Uses
RMMV
Java and C# must be compiled (even if it's JIT) and as far as I know Javascript can't be compiled it's "interpreted" by JS engine and that's why it is slower and has extra layer(s) before machine language.

Edit: Ok ok... Anything can be compiled if there's compiler for it, but at least I havn't seen compiler for JS :)
Actually every engine nowadays compile javascript into machine language. Take a look:
http://thibaultlaurens.github.io/javascript/2013/04/29/how-the-v8-engine-works/
https://developer.mozilla.org/pt-BR/docs/Mozilla/Projects/SpiderMonkey/Internals

It's basically because of the way the runtime is structured; the way C# works under the hood is a lot more streamlined and efficient than Java is, especially in terms of things like structs.
But as far as I know, java works directly with hardware for many things, without the need of interpreters or JIT compilation(at least I think that it's without). Could you tell me more?


Found this on Stack Overflow. It's confusing but seems to encapsulate the idea that it's...a complicated answer, with no clear consensus on what "interpreted" and "compiled" really mean...but C# is at least "less interpreted" than JS is.
https://stackoverflow.com/questions/8837329/is-c-sharp-partially-interpreted-or-really-compiled
It seems that while JS will always need an interpreter for everything, C# and Java won't. But still, some people say that C# is a lot more efficient than Java(On performance, because it seems that Java is a twice better when it comes to Power efficiency . Also Javascript is incredibly power-efficient IMO, being just a little bit bellow C#, and being 20x better than Ruby), like this this guy above, and I would like to find out how
 
Last edited:

ChampX

Veteran
Veteran
Joined
Aug 14, 2016
Messages
228
Reaction score
151
First Language
English
Primarily Uses
RMMV
I've worked as a professional software developer for a few years now, so while I don't know everything, this is my understanding.

First and foremost, no language will save you from bad code or bad optimization. Your C++ application can be slower than your JavaScript equivalent if you have poorly written unoptimized code. For compiled languages, the compiler can assist you and will do a better job than you mostly, but even with that it can't save you from all unoptimized code so speed will essentially first and foremost boil down to how well the programmer can make use of the language's best optimization features.

From what I recall, C# has interpreted properties, but the JIT compilation compiles it to byte code, then the CLR (common language runtime) which other .NET languages also use eventually compile it again to native code (your ASM or machine code).

Java, I believe is rather similar, but when compiled to bytecode, the JVM (Java virtual machine) then handles interpreting the code necessary to run on that given platform. Unlike C# and other compiled languages where the native code is built into a platform dependent package (such as an exe), Java however is not as it is built to run on any platform that has the JVM installed which then handles how it should run on that specific platform. As a result, Java will generally be slower if everything else is equal.

Which basically brings us to the point that JavaScript, while to my surprise learning it can be compiled, is almost always interpreted with that interpreter usually being the browser. Even if you package your application to a specific platform (such as an exe for windows) using node webkit, you will still be running a watered down version of chromium, the open source version of chrome, that will then interpret your JavaScript.

So all things being equal, the more native your code is and the closer you get to the hardware, the faster it is going to be run by the CPU. In doing so, however, we make the task of porting to other platforms a lot more tedious. Yes I can write a C++ application that will run on Windows, Mac, and Linux, but I may need to still add platform specific code and will also need to recompile each time on that target platform to generate the appropriate native code. JavaScript, however, I just write it once and any platform capable of running a browser will run my code and I don't have to do anything regarding ports (usually).

If we didn't like human convenience then we would all still be writing code in ASM, where you can get the fastest speeds, but the process will be miserable.
 

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
857
Reaction score
991
First Language
Finnish
Primarily Uses
RMMZ

Good point! ...forgot that already :)

But it still doesn't change the fact that JS is interpreted language.

Interpreter (in this case browser or it's module) is a program that directly executes instructions, without requiring them previously to have been compiled into a machine language program. It is just the nature of scripting languages "VB script", "JavaScript" or any other.

If speed is important there's "WebAssembly" thats some HC compiled web code (but don't know which browsers support 'WASM').
 

peq42_

Yeet
Veteran
Joined
Feb 5, 2016
Messages
543
Reaction score
327
First Language
Portuguese(BR)
Primarily Uses
RMMV
If speed is important there's "WebAssembly" thats some HC compiled web code (but don't know which browsers support 'WASM').
Hey that's an interesting thing. According to what i found in google, chromium started supporting webassembly since Chromium 57.

How do you imagine that it can impact RM MV(From 1.6 update which brings chromium 61) ? I mean, with WebAssembly in our games(as far as I know) we will be able to use C/C++[And other low level languages] codes. That allow all kinds of stuff to be done in MV, and performance to reach incredible levels.
 
Last edited:

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
857
Reaction score
991
First Language
Finnish
Primarily Uses
RMMZ
How do you imagine that it can impact RM MV ? I mean, with WebAssembly in our games(as far as I know) we will be able to use C/C++[And other low level languages] codes. That allow all kinds of stuff to be done in MV.

I think WASM is coming... But it's not ready yet. I mean this soup of browsers and users... there are quite a lot of things to take care of. And thus RMMV shouldn't use it yet. AND last but not least: One of the best things in RMMV is that it runs even on my Windows 8 Phone!!! Please don't take it from me! :kaodes:
 

peq42_

Yeet
Veteran
Joined
Feb 5, 2016
Messages
543
Reaction score
327
First Language
Portuguese(BR)
Primarily Uses
RMMV
I think WASM is coming... But it's not ready yet. I mean this soup of browsers and users... there are quite a lot of things to take care of. And thus RMMV shouldn't use it yet. AND last but not least: One of the best things in RMMV is that it runs even on my Windows 8 Phone!!! Please don't take it from me! :kaodes:
I don't mean adding stuff to be used by default(Not now at least. Also, there's support for webassembly on mobile, it was experimental in 2016 so must be on by default now), I was talking about plugins.

Using C/C++ and other languages it's possible to do SO MANY stuff. Maybe it will be finally easier to developers more skilled than me to make Plugins that work better with 3D, multiplayer,etc which everyone find so difficult to do now. And even stuff beyong that.
 

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
857
Reaction score
991
First Language
Finnish
Primarily Uses
RMMZ
it was experimental in 2016 so must be on by default now

Yes, WASM is already shipped with all modern browsers by default. If you don't need to care about users who are using older browsers then WASM is ok. And it still isn't working on my W8 phone :kaocry:

Edit: And it's still on "testing stage" no matter what they say. They are testing it with wide scale. For example it might be dangerous if you can execute pretty much any instruction... especially if exploiting vulnerabilities to get out of the sandbox (example: Intel) etc...
 
Last edited:

SamJones

Autorun
Veteran
Joined
Jul 12, 2015
Messages
113
Reaction score
47
First Language
English
Primarily Uses
RMMV
nobody says true, here why
Because
PHP:
"c#".length // return 2
and
PHP:
"javascript".length; // return 10
"java".length; // return 4
2 is faster than 10 and 4
:troll:
ASM - 3 is faster than 2
 

peq42_

Yeet
Veteran
Joined
Feb 5, 2016
Messages
543
Reaction score
327
First Language
Portuguese(BR)
Primarily Uses
RMMV
nobody says true, here why
Because
PHP:
"c#".length // return 2
and
PHP:
"javascript".length; // return 10
"java".length; // return 4
2 is faster than 10 and 4
:troll:
 

Neptrone

Veteran
Veteran
Joined
Apr 23, 2021
Messages
64
Reaction score
40
First Language
.
Primarily Uses
N/A
Have you checked this benchmark ? https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/javascript.html Javascript vs Java


https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/csharp.html Java vs C#

the Gap actually isn't really wide. And in other benchmark C# and Javascript speed is actually comparable.


Take a look other benchmark : https://github.com/kostya/benchmarks (This one pretty good benchmark for RL scenario)

There are some calc which node.js is faster than C#/Net.core or C#/Mono

https://programming-language-benchmarks.vercel.app/javascript-vs-csharp (this one good too)
 

pawsplay

Veteran
Veteran
Joined
Mar 29, 2012
Messages
495
Reaction score
265
First Language
English
Primarily Uses
RMMV
JS is just-in-time interpretation, which means that it is more interpreted, and can't use tricks compilers use for more runtime efficiency.

JS also has no native input/input. It always lives inside another environment, and so, for instance, your browser's performance is a limitation on the performance of JS run in your browser. The output of JS is essentially just a series of console messages. Technically JS can't do "hello world" without any extension provided by its runtime environment.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,637
Reaction score
1,463
First Language
French
Primarily Uses
RMMV
depending optimisation you do, is not so bad and sometime is faster than C
 

Latest Threads

Latest Profile Posts

ScreenShot_5_26_2022_11_36_32.png
added a Panic gauge to battle. it fills up according to your actions. each character has a different passive that activates once the gauge is full. also once full, a countdown starts and after each action taken, the gauges decreases. with each character, the decrease is different.
Genuinely considering making a thread informing people how to Google.
It's time for Da Update...

Prototyping some overworld abilities. Some of the characters you'll meet on your journey can use special overworld skills to travel around. This can be something like digging holes in the ground or getting hints towards your next objective.

Even more things I made.
Equipping Abilities is very important.
fLhHhZjh.gif

Forum statistics

Threads
122,157
Messages
1,146,895
Members
160,424
Latest member
SpaapS
Top