javascript - Use of `new Function` and performance issues - Stack Overflow

I'm loading a script file via AJAX, and to run its content I'm doing this:new Function('

I'm loading a script file via AJAX, and to run its content I'm doing this:

new Function('someargument',xhr.responseText)(somevalue);

However, according to MDN:

Function objects created with the Function constructor are parsed when the function is created. This is less efficient than declaring a function and calling it within your code, because functions declared with the function statement are parsed with the rest of the code.

I really don't quite get it. If a function is declared, it still has to be parsed from the string format of the file, so why would running a loaded string through new Function be any less efficient?

This is really more of a curiosity thing for me. I can understand why it would be bad in a loop (having to re-parse the same string), but for something like this I don't think there's any issue, is there?

I'm loading a script file via AJAX, and to run its content I'm doing this:

new Function('someargument',xhr.responseText)(somevalue);

However, according to MDN:

Function objects created with the Function constructor are parsed when the function is created. This is less efficient than declaring a function and calling it within your code, because functions declared with the function statement are parsed with the rest of the code.

I really don't quite get it. If a function is declared, it still has to be parsed from the string format of the file, so why would running a loaded string through new Function be any less efficient?

This is really more of a curiosity thing for me. I can understand why it would be bad in a loop (having to re-parse the same string), but for something like this I don't think there's any issue, is there?

Share Improve this question asked May 8, 2013 at 1:31 Niet the Dark AbsolNiet the Dark Absol 325k85 gold badges474 silver badges600 bronze badges 5
  • 2 It's parsed almost like eval is, which is known to be slow. I suppose it should be a little faster, as is doesn't have to hook up the scope chain. – bfavaretto Commented May 8, 2013 at 1:36
  • Don't send functions by AJAX, send data. Have your functions defined in a .js or <script>. – Paul S. Commented May 8, 2013 at 1:40
  • @PaulS. This is intended for a preloader script. – Niet the Dark Absol Commented May 8, 2013 at 1:41
  • Also, this is what happens when that is evaluated: es5.github.io/#x15.3.2.1. That happens at runtime, so it could be perceived as even slower. – bfavaretto Commented May 8, 2013 at 1:42
  • 1 In this particular case, which is actually an interesting use of Function(), it doesn't matter and I doubt it could be done in another way. – Ja͢ck Commented May 8, 2013 at 1:48
Add a ment  | 

3 Answers 3

Reset to default 6

I think what they are saying is that if you use the function constructor in your code like this:

new Function('bar', 'console.log(bar);'));

The function body is parsed twice: the first time as a string when the code is loaded, and the second time when the function is constructed at runtime. In your case, you are creating the function from an ajax response after the code has been parsed, so really its a whole different deal.

I think the MDN docs are referring to something like this:

var f = new Function("return 5;");

As opposed to:

function f() { return 5; }

The former version is less efficient because it first creates an actual String object ("return 5") in JavaScript and then parses that string to create a Function object. The latter parses the code without an intermediate string.

That said, in your case since you're loading the JavaScript code dynamically I don't think there's really any getting around it.

I don't know what the author of that MDN article intended, but here is one interpretation.

Many modern JS interpreters use an optimizing piler to produce native code.

For example, "JavaScriptCore, the WebKit JS implementation" says:

In that case there is tiered pilation between the three forms: initial parsing and pilation produces bytecode, that can be optimized with the method JIT, that can be optimized by the DFG JIT. In practice, though, on most platforms the interpreter is not included, so that all code runs through the method JIT.

The more plete the picture an optimizing piler has of the code it's piling, the more optimizations it can perform, so the more highly optimized the function can be. For example, if you know that every reference to a function is used to call it immediately with a string as its sole argument because it is defined in a strict function body, then you might be able to avoid allocating a function object for it at all and perform certain optimizations in its body.

When you call new Function, the optimizing piler does not get the context necessary to do those and other optimizations.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744701399a4588798.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信