Silverlight managed code vs. JavaScript

I finally had a chance to play with Silverlight today. The managed code capabilities is very interesting, so I set out to create a simple performance tester.

I created two basic managed methods in Silverlight. One that multiplies a number and returns a result, and another that does the same thing a specified number of times:

[Scriptable]
public double Multiply(double a, double b)
{
   return a * b;
}
[Scriptable]
public double MultiplyLoop(double a, double b, int count)
{
   double result = 0;
   for (int i = 0; i < count; i++)
      result = a * b;
   return result;
}

This gives me 3 test-cases to compare:

  • Multiply two numbers n times in pure javascript.
  • Multiply two numbers n times, by calling Multiply(a,b) from JavaScript into managed code n times
  • Multiply two numbers n times, by calling MultiplyLoop(a,b,n) from Javascript into managed code one time.

What I found was:

  • JavaScript is fairly slow to do this (345 ms for 10 millions calculations).
  • Calling a managed method millions of times is VERY slow (IE asked me several times whether I wanted to abort it before it finished)
  • Calling a managed method once and do the looping in managed code is VERY fast (0-2 ms).

The conclusion here would be that you shouldn't have methods in managed code that you call a lot from javascript unless they are process-intensive. There seems to be a big overhead in calling into managed code, but as soon as you are in the managed process, its blazing fast.

You can download my test sample here: SilverlightProject1.zip (18.35 KB)  (requires Orcas beta 1 and Silverlight add-on)

Now on to move all my existing .NET libraries into a Silverlight app... :-)

Getting (re)started with Silverlight and some notes

On my way back from the Mix conference in Vegas I was trying to get one of my WPF/E applications to run on the new Silverlight 1.1alpha. It wasn't as easy as I thought it would be because a lot seems to have changed. I finally figured it out after reading Bryant Likes's blog. He already figured it out and shared the solution (Thanks Bryant!):

If you want to go the "real" route of working with Silverlight 1.1alpha, here's what you need:

I saw two really good sessions on Silverlight and .NET this morning. They are online now, and I really recommend that you watch them:

One of the really neat things are that Silverlight also supports LINQ in the client browser. Part 2 shows a cool example on that and how easy it makes it to work with data. Furthermore you can easily communicate between Browser DOM, Javascript and .NET assembly code which makes for some interesting scenarios. Since .NET excution is 400-1000 times faster than JavaScript, you could have your heavy algorithms inside silverlight, and call them from Javascript. If you don't need the presentation-layer, you could just hide it and have it as a library you utilize from JS.

So what is the main differences between v1.0 and v1.1? v1.1 contains:

  • Everything from v1.0
  • A subset of the .NET 3.5 framework and some extra browser and AJAX specific classes
  • XAML extensibility
  • Control class (for user controls)
  • Sample controls

Currently Silverlight works with Safari, Firefox and IE on both Mac and Windows and soon to be Opera as well. I asked them whether they would also support other platforms like Linux. Basically they didn't have any 'issues' with making that, but were currently not considering it until there is a larger demand for it.

Update: Mono has already announced that they will make sure we get Silverlight support on Linux. COOL!

Mix07 Keynote

The big topic of today’s keynote by Ray Ozzie & Scott Guthrie at the Mix07 was Silverlight. Today several new things were unveiled:

  • Silverlight 1.0 beta was released
  • Silverlight 1.1 alpha was released / unveiled
  • Silverlight Streaming was unveiled
  • Silverlights new community website was unveiled at www.silverlight.net
  • Blend Expression was released to day (we all got a free copy too)

And to me the biggest news of them all:

  • Silverlight 1.1 alpha has support for .NET code inside the Silverlight application!

Basically this means that you now can use C#, VB, IronPython, the all-new IronRuby (also unveiled today) and any other .NET language in an app that runs inside the browser crossbrowser AND crossplatform. Through .NET you can also work with and manipulate the HTML DOM, use REST services etc. No more JavaScripting! (and I was just about getting to a point where I really started to like JavaScript)

Scott Guthrie demoed a cool Silverlight-based chess application that used both JavaScript and C# for the AI, and had the two languages “compete” against each other. According to Scott, .NET is 400-1000 times faster than JavaScript, so it could calculate a lot more moves in the same time. Of course .NET won the simulated game :-) There were a lot of other cool Silverlight demos by non-Microsoft companies, including a kick-ass video-editing app running in your browser.

“Silverlight Streaming” (http://silverlight.live.com/) is Microsofts new website for sharing video (A Youtube killer perhaps?). You can freely sign up for 4gb space, and supports up to DVD-quality videos of up to 10mins. You can completely integrate videos streaming from Microsofts servers onto your webpage through Silverlight. So the next time you upload a really cool video to your website with the risk of overloading your server, Silverlight Streaming might be your answer.

So, Silverlight 1.0beta is out now, and will be finally released in the Summer 2007. 1.1alpha is out now too, but the final release is still to be determined. This includes a subset of the .NET framework, similar to what they did to the compact framework.

They also showed that through Visual Studio Orcas you can now remotely debug Silverlight applications that run on a Mac, set breakpoints, change properties on the fly etc. REALLY cool stuff.

Hi I’m a Mac. Hi I’m a PC and I can control you remotely! ;-)

You can download Silverlight 1.0b, 1.1a and SDKs at http://msdn2.microsoft.com/en-us/silverlight/default.aspx

So basically this means you now can use .NET for a larger array of purposes:

  • ASP.NET (Webserver)
  • XNA (XBOX)
  • CF.NET (Pocket PC/Phone)
  • Silverlight (Webclient – Crossplatform!)
  • WPF (Desktop)

I can’t wait to get started with this!

The other sessions I attended today was pretty good too (although missing most of the one I wanted the most because the hotel got my reservation screwed up), none of it was really new stuff we haven’t seen before. Hopefully there will be some more WOW tomorrow…