It can also add new variables to your scope which defeats a number of optimisations. In reality not sure there would be much of a performance difference would there? The 15 milliseconds for 1K LOC might not be accurate, depending on your test machine. As soon as I read that, my mind went back to this blog post:. Darryl, yes the IE test was run on XP, but it was averaged over 5 runs.
According to the link you cite, xp may be rounding down to the nearest 15ms. So at worst we can say the time is closer to but less than 30ms. These two are not equivalent. It switched to the Function constructor because eval of a large object gets very slow when Firebug is present. They are essential and powerful tools when used properly — for example, when you write code that writes code.
Think of a JavaScript template engine that compiles the template to native JavaScript code for performance. But as you point out, in most of the places where people use eval, there are cleaner and better ways of doing it. Thanks Michael, good explanation — I know Function was used as early as 1.
How evil is this page? I have opera set to open the error console on js errors, and this page generates errors on mousemove events! Thanks for a thoughtful article.
I use eval specifically for parsing information generated and sent from my server. Now I know. You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account.
You are commenting using your Facebook account. Notify me of new comments via email. In most cases of this nature, you can replace the call to eval by using bracket notation to construct the property name that is, after all, one reason it exists.
Those early bloggers who talked about misuse, Crockford included, were mostly talking about this pattern. A good reason to avoid eval is for debugging purposes. Until recently, it was impossible to step into eval ed code if something went wrong. That meant you were running code into a black box and then out of it. You have to wait until the code executes once before it shows up in the Source panel.
Avoiding eval ed code makes debugging easier, allowing you to view and step through the code easily. Another big hit against eval is its performance impact. In older browsers, you encountered a double interpretation penalty, which is to say that your code is interpreted and the code inside of eval is interpreted.
The result could be ten times slower or worse in browsers without compiling JavaScript engines. Most engines can run code in one of two ways: fast path or slow path. Fast path code is code that is stable and predictable, and can therefore be compiled for faster execution. Slow path code is unpredictable, making it hard to compile and may still be run with an interpreter 3. Also of note, eval makes it impossible for YUI Compressor to munge variable names that are in scope of the call to eval.
Since eval can access any of those variables directly, renaming them would introduce errors other tools like Closure Compiler and UglifyJS may still munge those variables — ultimately causing errors.
So performance is still a big concern when using eval. Once again, that hardly makes it evil, but is a caveat to keep in mind. The compiler prepares the machine code for variables of certain value type.
When you use variables from outside of the eval inside of it, the browser is forced to do a timely-expensive variable lookup to check if a variable exists and what is its type. Chances are you are familiar with the JSON. Before that, json2. If you look closely at its source code you will notice eval! It is quite funny because in his code conventions you can read:.
So is eval completely useless? If you know what you are doing, no. The JSON. A good one is Webpack. In my article Webpack 4 course — part six. Eval-source-map is one of the possible configurations. With it, the browser executes each module using eval and the source map is added accordingly. So in this example, the browser calls the eval function that Webpack generated. This article went through the basics of how eval works.
It included explaining what are the dangers of using JavaScript eval , taking into consideration how JavaScript is executed by the browser interpreting or being compiled. The article also pointed out some real-life usages of eval , like the source maps generated by Webpack. Hopefully, this will help you in deciding whether you need to use eval, or not. Previous article Handling errors in JavaScript with try Next article Using recursion to traverse data structures.
0コメント