是否像eval一样未通过V8优化Function()构造函数? [英] Is the Function() constructor not optimized by V8, like eval?

查看:140
本文介绍了是否像eval一样未通过V8优化Function()构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试通过WebSockets接收Web组件的方法.这些组件包含自定义脚本,它们应该在组件内部的上下文中运行.

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.

现在,我们正在为此使用eval,如下所示:

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

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

并可以按预期工作,但是我正在读取包含eval的任何函数均未通过V8优化.我想像这样将其转换为new Function():

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);

这样,我可以实现与上面的ctxEval函数相同的功能.

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

我们知道Functioneval(),因为它们的行为几乎相同,但是现在的问题是,直到Functioneval()哪一点?可能是因为Function()具有自己的作用域,而不是eval在同一作用域中运行代码的作用域,所以包含Function调用的函数实际上是由V8优化的.另外,在此处,他们谈论的是eval而不是Function构造函数

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.

另一个隐含的问题是,在Function()内部运行的脚本是否已通过V8优化?

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

推荐答案

我刚刚使用这段代码对此进行了测试

I just tested this with this code

const adder = new Function('a', 'b', 'return b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b');
let b = 0, b2 = 0;
function _throw() {
    throw new Error('Ups');
}
function _catch() {
    try {_throw()} catch(e) {}
}
function printStatus(fn) {
    switch (%GetOptimizationStatus(fn)) {
        case 1: console.log(fn.name, "function is optimized"); break;
        case 2: console.log(fn.name, "function is not optimized"); break;
        case 3: console.log(fn.name, "function is always optimized"); break;
        case 4: console.log(fn.name, "function is never optimized"); break;
        case 6: console.log(fn.name, "function is maybe deoptimized"); break;
    }
}
eval('function evil(a,b) {return b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b}');
printStatus(adder);
printStatus(evil);
printStatus(_throw);
printStatus(_catch);
// Call the function
for(let i = 0; i < 2000; i++) {
    b = adder(Math.random() * 10, b);
    b2 = evil(i, b2);
    _catch();
}
printStatus(adder);
printStatus(evil);
printStatus(_throw);
printStatus(_catch);

运行命令

$ node --allow-natives-syntax js.js

输出为

anonymous function is not optimized
evil function is not optimized
_throw function is not optimized
_catch function is not optimized

anonymous function is optimized
evil function is optimized
_throw function is not optimized
_catch function is not optimized

我修改了此测试代码以检查其他诱饵,并真的感到很惊讶,因为它似乎也优化了eval:>

I modified this test code to check other bailots and im realy surprised because it looks that eval is also optimized :>

经过一些额外的研究,我发现了这个 https://blog.sqreen.io/optimize-your-node-app-by-simply-upgrading-node-js/

After some additional research I found this https://blog.sqreen.io/optimize-your-node-app-by-simply-upgrading-node-js/

这篇关于是否像eval一样未通过V8优化Function()构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆