Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance and memory differences between C# and Javascript? [closed]

We have a C# winforms application which models a 3D globe and world state using a large number of object instances, float[] arrays and object references to represent the world state and relationships between objects.

We have been asked to migrate this software to the web and reimplement it in Javascript.

I understand that C# gets jitted to native code, however it sounds as though in recent years there have been huge advances in Javascript performance too.

I am wondering if there is any general information or comparisons of how Javascript fares, performance and memory wise, for raw data manipulation of objects and arrays, compared to .NET or other languages which execute at native performance?

like image 595
Brendan Hill Avatar asked Feb 17 '16 13:02

Brendan Hill


People also ask

Which is faster C or CPP?

C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.

Does C use less memory than C++?

There is no real difference between C and C++ in terms of memory allocation.

What is memory in C programming?

A C program memory layout in C mainly comprises six components these are heap, stack, code segment, command-line arguments, uninitialized and initialized data segments. Each of these segments has its own read, write permissions.

What is the main difference between memory and storage?

Memory is what your computer uses to store data temporarily, while storage is where you save files permanently. When you save a file, it's copied from the memory onto the storage drive. This is why your computer seems to run slower when it's low on memory; it has to pull data from the storage drive in order to use it.


1 Answers

Short Answer

If you are a proficient C# developer and novice JavaScript developer - your C# will most certainly be faster. If you are proficient at both then your C# will probably be faster, but the difference may not be as much as you thought - this is all very program specific.

Longer Answer

C# and JavaScript are languages so they don't have any particular performance characteristics themselves. C# compiles to .NET IL and is executed in a virtual machine, and various optimizations can be in play (such as the JITing you mentioned). JavaScript is not compiled, but rather interpreted - and is done so by the JavaScript engine particular to the browser. Each browser may have different approaches to improve "performance" of executing JavaScript - but performance optimization typically involves trade-offs (between speed and memory, for instance).

With everything else being equivalent (and non-trivial), the .NET code - JITed or not - will perform better than analogous JavaScript code running in a browser. The degree of difference in performance is highly specific to the particular program. Everything from the size and number of objects being processed down to how and when you use loops will affect how one runtime compares against the other.

More Details

Developers are sometimes confused about which languages are interpreted or compiled, and many are under the impression that the two are exclusive. In reality the situation is a bit more complicated.

  • C# for instance is compiled (into IL byte code). The IL Byte code is then interpreted (and typically JIT compiled by the particular .NET runtime).
  • C# contains features such as dynamic and Reflection.Emit() which enable C# to be used more like a scripting language while also "circumventing" the compiler and some of the performance benefits it provides.
  • JavaScript is interpreted, but a JavaScript engine is completely free to JIT as it sees fit. Check this blog article about Firefox where they describe how they use a two-phase JIT approach.

In the "real world" (non-trivial code, standard compilers and standard settings) compiled code will run faster than equivalent "pure" interpreted code. However, nowadays interpreted code usually runs through a JIT compiler - which has the possibility to be faster than pre-compiled code (because the JITter could tailor compilation to take advantage of a particular instruction set, for instance). Languages such as C# and Java take advantage of both compilation and "interpreted-JIT-compilation" by compiling down to byte code and then running through a JITer during execution.

The handicap that JavaScript faces in the performance arena has nothing to do with compilation but rather in certain aspects of the language and how it is (or tends to be) used. Again if you take a look at the blog entry on Firefox you see how changes to type information need to be tracked by the baseline compiler (which is better than the prior model of discarding the associated JIT instructions entirely).

With all this taken into consideration, well written JavaScript on a modern browser will run extremely well. It will still suffer the performance hit of the initial JIT pass (the equivalent of compiling to byte code before the "real" JIT), but the engines are written to minimize this as much as possible.

like image 60
Ethan Cabiac Avatar answered Sep 22 '22 03:09

Ethan Cabiac