We are trying to get web components through WebSockets. These components contain user scripts and should be run in context within the component.
In short, we have several script lines and want to run them.
Now we use eval to do this, something like this:
function ctxEval(ctx, __script) { eval(__script);
and it works as expected, but I read that any function containing eval is not optimized by V8. I decided to convert it to new Function() as follows:
new Function("ctx", __script)(ctx);
this way I can achieve the same as ctxEval above.
We know that Function is eval() , because they act almost the same, but now the question is, to what point is Function eval() ? Perhaps because Function() has its own scope instead of eval , which runs code in the same scope, the function containing the Function call is actually optimized by V8. Also, here they talk about eval , but not the Function constructor.
And another question implied in this is a script that works inside Function() optimized with V8?
source share