Tag Archives: JavaScript

CoreCLR vs JavaScript

JavaScript is the technology that dies very hard. After several hard years of crazy optimizations by Microsoft, Apple, Google, etc. please welcome a new lap of JavaScript improvements called TypeScript.

I will not explain what TypeScript is, neither will I predict its future. What I want to show, is how easy to port code from TypeScript to C#.

As a bonus, we’ll have some insight about JavaScript performance.

So what I basically did? I took TypeScript Raytracer demo, found at TypeScript playground here, and ported it to C# without any algorithmic changes.

I was wondering how different will it run under technology with “shifted strategy”. I know that desktop .NET CLR is more mature and optimized than Silverlight CoreCLR. But let’s just try to compare client stack technologies. One which is 5 yeas old and second is 18 years old.

Here is a link to TypeScript playground, select there Raytracer demo to continue. You just need to make small change in TypeScript code to make things more obvious. Change the bitmap dimensions to 1000 x 1000. Here is a snippet to replace exec() function:

function exec()
{
  var canv = <HTMLCanvasElement>document.createElement("canvas");
  canv.width = 1000;
  canv.height = 1000;
  document.body.appendChild(canv);
  var ctx = canv.getContext("2d");
  var rayTracer = new RayTracer();
  return rayTracer.render(defaultScene(), ctx, 1000, 1000);
}

Depending on web browser you are using, results may vary. In IE10, for example, on all my desktop and notebooks this takes ~ 30 seconds. In latest Chrome ~ 40 seconds (probably because of Frozen Script dialog).

Now, when you’ve checked your own system, we may continue with CoreCLR. Below is a link to the demo. How much faster does it work?

On my system CoreCLR is roughly 20 times faster than best JavaScript contender. And after I applied some scaling techniques, we get whopping 30x performance boost.

Here is the guy: Raytracer Silverlight Demo (1000 x 1000 pixels) (click Render button in bottom right corner to render)

Just to make it clear, JavaScript works in your browser, in most of built-in Windows 8 apps, in your mobile, in your tablet. It effectively burns device batteries.

Imagine just for a second, what if all apps written in JavaScript would be replaced by C# analogues, how much longer will your device last without recharge?

How many kWh/trees/oil/coil/$$$/€€€ could we spare?