Compile and loops

Apr 11, 2011 at 12:59 PM

First, thanks for the great project!

Question: when using a bit of JS code inside a .Net loop, is there a way to improve speed? I mean, the Execute inside the loop probably does it with some overhead (only variables change, not the script). The way I use it now is via a rendering/animation loop which draws some stuff specified in the JS script.

Thx for your help, Fr.

 

Coordinator
Apr 12, 2011 at 2:58 AM

Good question!

This is all off the top of my head so forgive any errors, but the following should be substantially faster than calling Execute repeatedly.

scriptEngine.Execute("function MyFunc() { /* js code goes here */ }");
for (int i = 0; i < 1000; i ++)
{
    ((Jurassic.Library.FunctionInstance)scriptEngine.GetGlobalValue("MyFunc")).Call(null);
}

 

Coordinator
Apr 12, 2011 at 8:25 AM

Even better:

scriptEngine.Execute("function MyFunc() { /* js code goes here */ }");
var myFunc = (Jurassic.Library.FunctionInstance)scriptEngine.GetGlobalValue("MyFunc");
for (int i = 0; i < 1000; i ++)
{
    myFunc.Call(null);
}

Coordinator
Apr 12, 2011 at 8:30 AM

Note that you can make the function take some variables and vary what gets executed based on what you passed in:

scriptEngine.Execute("function MyFunc(a) { console.log(a) }");
var myFunc = (Jurassic.Library.FunctionInstance)scriptEngine.GetGlobalValue("MyFunc");
for (int i = 0; i < 1000; i ++)
{
    myFunc.Call(null, i);
}
Apr 12, 2011 at 11:33 AM

Works well and solves at the same time another question I had, namely how to access separate JS functions.

At this point I have a pretty clean and working prototype of the popular "Processing.js" project but on top of Silverlight and your project. If you're interested I can send you a zip.
Probably some more optimizations would be good on different levels (the rendering and the JS translation) but for now, thanks for your kind help and hard work.

 

Fr.

Coordinator
Apr 13, 2011 at 8:41 AM

Sounds like a cool project :-)  I'd be interested in having a look - send the zip to paulbartrum at hotmail.com.