Skip to content Skip to sidebar Skip to footer

Is The Function() Constructor Not Optimized By V8, Like Eval?

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 s

Solution 1:

I just tested this with this code

const adder = newFunction('a', 'b', 'return b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b');
let b = 0, b2 = 0;
function_throw() {
    thrownewError('Ups');
}
function_catch() {
    try {_throw()} catch(e) {}
}
functionprintStatus(fn) {
    switch (%GetOptimizationStatus(fn)) {
        case1: console.log(fn.name, "function is optimized"); break;
        case2: console.log(fn.name, "function is not optimized"); break;
        case3: console.log(fn.name, "function is always optimized"); break;
        case4: console.log(fn.name, "function is never optimized"); break;
        case6: 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 functionfor(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);

run command

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

and the output is

anonymous functionisnot optimized
evil functionisnot optimized
_throw functionisnot optimized
_catch functionisnot optimized

anonymous functionis optimized
evil functionis optimized
_throw functionisnot optimized
_catch functionisnot optimized

EDIT:

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

EDIT 2:

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

Post a Comment for "Is The Function() Constructor Not Optimized By V8, Like Eval?"