For
eval, two cases need to be distinguished: functions containing an
eval statement, and functions defined inside eval'ed code. Consider:
function foo() {
/* lots of stuff... */
eval("function bar() { /* lots of stuff... */ }");
/* more stuff... */
}
foo();
bar();
In this case, function foo will experience a slowdown because of its eval statement. Back in the Crankshaft days, it wasn't optimizable at all; now with Turbofan, it can be optimized if it's hot, but the presence of eval disables some optimizations (which is unavoidable, due to what eval can do -- an optimizing compiler simply can't eliminate as much stuff as it could without eval). If foo only runs once (as is typical for setup code), then this doesn't matter at all, as functions won't get optimized on their first execution anyway.
Function bar, on the other hand, generally[1] doesn't care whether it came from an eval statement or someplace else. IIUC this is what you're interested in, and why you're not seeing a performance difference between using eval or a v8::Script.
[1] The (obvious?) exception is if the eval statement is executed repeatedly, possibly even with dynamically computed strings, then of course there is parse/compile work going on every time, and depending on what you're doing there the functions generated that way might not live long enough to ever get optimized.
So, some performance advice still applies, and will very likely always apply:
- don't put eval statements into hot functions
- prefer indirect/"global" eval over plain eval
- use eval only when you have to (especially for websites: various things like prefetching, streaming, caching work best without eval; if you unnecessarily eval code you'll probably miss out on some benefits. Other embedders of V8 might not care about this.)