Is the Function() constructor not optimized by V8, like eval?

Viewed 1117

We are trying a way to receive web components via WebSockets. Those components contains custom scripts and they should be run in a context inside the component.

In short, we have some script strings and want to run them.

Right now we are using eval for this, something like this:

function ctxEval(ctx, __script) {
    eval(__script);
    // return things with the ctx
}

and works as expected, but I'm reading that any function containing eval is not optimized by V8. I thought into converting it to new Function() like this:

new Function("ctx", __script)(ctx);

this way I can achieve the same as the ctxEval function above.

We know that Function is eval() because they act almost equally, but the question now is, until which point Function is eval()? Maybe because Function() has its own scope instead of the eval one which runs the code in the same scope, the function containing the Function call is actually optimized by V8. Also, here they talk about eval but not about Function constructor.

And another question implied in this one is, is the script that runs inside Function() optimized by V8?

1 Answers
Related