EvaluateCode and its Reuse

I have a case, Context.EvaluateCode<object>(someText) that is being called in a loop and someText does not change. I noticed that underneath this calls OneTimePage which means it gets re-evaluated each time since that creates an in memory new file. Is there a better more performant way to do this?

The #Script Code docs shows the finer grained control, e,g:

var context = new ScriptContext().Init();
var dynamicPage = context.CodeSharpPage("now |> dateFormat('HH:mm:ss')");
var output = new PageResult(dynamicPage).RenderScript();

Instead of accessing the script’s output you can access the return value with:

var result = new PageResult(dynamicPage).EvaluateResult(out var returnValue)
    ? ScriptLanguage.UnwrapValue(returnValue)
    : null;

If the source code doesn’t change can reuse your dynamicPage which captures the parsed AST of your source code.

1 Like

I think this added about 30% more performance in my current scenario. Thanks.

1 Like