Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Does 'eval' has the same performance as an explicit Script->Compile->Run pipeline?

455 views
Skip to first unread message

Al Mo

unread,
Apr 14, 2019, 5:28:03 PM4/14/19
to v8-users
Let's say I have some code and I want to execute it, you could:

(a) create a v8::Script, compile it and then run it,

OR,

(b) send the string to an active v8::Context and call eval(code) from inside.

I remember hearing that eval does not optimize code, but in my purely empirical tests both scenarios seem to run code at about the same speed.

But still the question is, are there differences in performance between the two approaches? What other things should I expect to be different?


Al Mo

unread,
Apr 14, 2019, 5:36:48 PM4/14/19
to v8-users
I just found this: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers

So that kind of answers my own question. Still, if anyone wants to drop its two cents, I'll be glad to read.

Still puzzled why I 'measure' the same performance on both cases. I'm doing millions of linear algebra operations. Also compared that to similar C and performance is pretty close. If this means they could be even faster then wow :D

Mathias Bynens

unread,
Apr 15, 2019, 2:03:43 AM4/15/19
to v8-users
The document you linked to is outdated. It was based on Crankshaft which hasn't been used since V8 v5.9.

See the first two lines:

All this is wrong in TurboFan. (Node 8+)
Please do not take anything written here as performance advice, this is here for historical reasons.

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jakob Kummerow

unread,
Apr 15, 2019, 4:38:36 AM4/15/19
to v8-users
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.)

Reply all
Reply to author
Forward
0 new messages