Skip to content

Fix #1832: Browser compiler should run code using globally-scoped eval #5047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 1, 2018

Conversation

GeoffreyBooth
Copy link
Collaborator

Fixes #1832. The top of that thread suggests that we use script (DOM) injection rather than plain eval to execute scripts that users enter in browser contexts, such as Try CoffeeScript, to boost performance dramatically. By the end of the thread, it’s established that the reason script injection is so much faster is that we were using eval in the context of the CoffeeScript compiler; if we use window.eval instead, to use a global context and exclude all the local variables accessible where we’re making the eval call, the window.eval method is ever so slightly faster still than script injection. It’s also compatible with Node, so cake test:browser still works (if we first detect whether to call window.eval or global.eval). The performance tests are still accessible here: http://web.archive.org/web/20130326103111/http://jsperf.com:80/eval-vs-script/6

The other changes you see in this PR turn off variable name mangling in our minification, which was previously happening because the presence of the eval call was automatically disabling mangling in the scope that eval had access to; now that we’re using a globally-scoped eval, that no longer happens, and the browser compiler breaks when full mangling is enabled. And finally, one of the browser-based tests was broken by #5028, and I included the fix here.

So yes, in the simplest sense #1832 could’ve been resolved seven years ago by adding just seven characters (changing eval to window.eval). Sorry it took so long.

@GeoffreyBooth GeoffreyBooth merged commit d82272b into jashkenas:master May 1, 2018
@GeoffreyBooth GeoffreyBooth deleted the script-injection branch May 1, 2018 15:11
@GeoffreyBooth GeoffreyBooth mentioned this pull request May 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance: <script> injection is faster than eval
1 participant