Most efficient way to execute dynamically defined function

12 views
Skip to first unread message

andy7...@gmail.com

unread,
Jan 1, 2012, 7:31:22 AM1/1/12
to iro...@googlegroups.com
Hi,

I was playing around with IronJS, trying to allow a Javascript filter function be defined dynamically (as a string in .Net) and be applied to some piece of JSON data (again, coming in as a string in .Net). However, I'm not seeing the performance I would need for what I had in mind - and that may just be the limit of the DLR etc, and that's fine. But before I drop the idea of using a IronJs, I just wanted to be sure I wasn't doing anything silly

I've included my spike code below. On my machine, it executes the function about 400 times a second. Is there a more efficient way to do this?

Thanks in advance


let testIronJs(count) = 
    let testMessage = @"{ 'someData':'blah' }"
    let testPredicate = @"function(data) {
        return data.someData === 'blah';
    }"
    let ctx = IronJS.createContext()
    let sw = new Stopwatch()
    sw.Start()
    // just defining function in global scope for quick test
    let fn = sprintf "testFn=%s;" testPredicate
    ctx |> execute fn |> ignore
    for i in 1..count do
        let script = sprintf "testFn(%s);" testMessage
        let result = ctx |> executeAs<bool> script
        if result <> true then raise(new Exception("Unexpected result"))
        ()
    sw.Stop()
    sw.Elapsed

let main() = 
    let count = 1000
    let timeTaken = testIronJs(count)
    printf "%d in %s\n%f/sec\n" count (timeTaken.ToString()) ((float count/timeTaken.TotalMilliseconds) * 1000.0)
    ()

main()

Fredrik Holmström

unread,
Jan 2, 2012, 10:05:38 AM1/2/12
to iro...@googlegroups.com
I would fetch the actual function object testFn and invoke it directly instead of creating a javascript string that calls it every time, as this will cause the parser to get invoked every time which is probably the root of the problem.

Regards,
Fredrik Holmström
Reply all
Reply to author
Forward
0 new messages