This project has moved. For the latest updates, please go here.

Praise and a couple questions

Sep 9, 2015 at 2:33 PM
Wow, this is an awesome project! I wrote a home automation system and implemented a simple rules system in XML (essentially If-Then-Else statements). As I wanted to do more complex things in my rules, I decided to look around for a way to host a scripting language directly so that I don't end up writing one over time. ClearScript does this so easily that I am just amazed. Thank you for releasing the project and for all the support you give in these forums; they have been a great resource for me.

So, 2 questions:

1) I noticed that when calling (from .NET) a JavaScript function that talks to .NET objects, it seems to take about an extra 100ms on the first call. Is this because the proxies are set up on demand? Just curious.

2) I want to have an implementation of some XmlHttpRequest features. As discussed previously in some topics here, this can easily be done by wrapping WebClient and I see how to do this on both sides of the fence. Is there a performance or garbage collection contrast between these 2 approaches:
  • Expose WebClient and URI (for the async method) to JavaScript and write a JS wrapper that creates .NET objects, subscribes to events, and performs the callback.
  • Write a C# wrapper around WebClient and URI and expose that 1 object to JavaScript.
Really this is a specific example of a general question - is it better to do such work on the C# side and expose simpler objects and interfaces to JavaScript?
Coordinator
Sep 9, 2015 at 10:07 PM
Edited Sep 9, 2015 at 10:07 PM
Hi ScytaleZero,

First, thank you very much for your positive feedback!

Now, on to your questions:

I noticed that when calling (from .NET) a JavaScript function that talks to .NET objects, it seems to take about an extra 100ms on the first call. Is this because the proxies are set up on demand? Just curious.

Most likely it's because invoking a .NET method from script code involves an expensive step (called binding) that maps the method name to a unique method. For an overloaded method this step may require an examination of the argument types, and for a generic method it may require the on-demand construction of the specialized (closed generic) version of the method.

Once a given call signature has been bound successfully, ClearScript caches the binding to speed up subsequent calls. This is very similar to what happens at dynamic call sites emitted by the C# compiler.

Really this is a specific example of a general question - is it better to do such work on the C# side and expose simpler objects and interfaces to JavaScript?

If you're concerned about performance and/or wish to restrict the script's activities, then exposing a simple, custom API is probably the best approach. The key for performance is to design your API to minimize hops across the .NET-script boundary, especially if you're using V8. That script engine is remarkably fast but not as fast as JIT-compiled C#, and the overhead of invoking native code from .NET is significant.

Good luck!