4

According to this benchmark http://jsperf.com/function-vs-function created functions run about 1000 times faster. Can you comment this?

oezi
  • 51,017
  • 10
  • 98
  • 115
Dan
  • 55,715
  • 40
  • 116
  • 154

2 Answers2

5
  1. You are calling f1 but not f2. I.e. your second test is doing nothing but looking up a reference.
  2. All the work is actually done as setup for the test.

I think what you want is actually this: http://jsperf.com/function-vs-function/2 Update: On second thought, you might not want this. But nevertheless, your second test is doing nothing. You are missing the () after f2 ;)

So besides new Function being way slower, it is also harder to maintain the body of the function ;)

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3

with the new Function-syntax, for every function the JS-compiler has to be started to "eval" the function body string - this is slow and should be avoided when possible:

Each time […] the Function constructor is called on a string representing source code, the script engine must start the machinery that converts the source code to executable code. This is usually expensive for performance – easily a hundred times more expensive than a simple function call, for example. (Mark ‘Tarquin’ Wilton-Jones)

if you had used the search on StackOverflow, you would have found this question wich give very good and detailed information about that.

EDIT: like Martin said in one of the comments below, sometimes the new Function-constructor is a great thing. to list some examples:

but: in 99% of the cases where you could use new Function, it's a bad idea - wich means: to simply define any function that has to be like it is and doesn't have some kind of "dynamic bahavior", you should always use the "normal" function-syntax to speed up your code and avoid the eval-like functionality of new Function.

Community
  • 1
  • 1
oezi
  • 51,017
  • 10
  • 98
  • 115
  • but new Function runs even faster! Do you also see what I see? – Dan Jan 20 '11 at 09:15
  • when i run you tests, i get "10,841,858 / fastest" for `function(){}` and "75,310 / 99% slower" for `new Function` - don't know what you're talking about. – oezi Jan 20 '11 at 09:18
  • 2
    Using ´new Function´ is not bad practice - it is an awesome tool, to be used when it is needed - people using it when it is not needed, that is bad practice. Saying that using ´new Function´ is bad practice is like saying "driving a car is bad practice", because some people run other people over while driving. That said, new Function is slower than a regular function and will always be, end of story. – Martin Jespersen Jan 20 '11 at 09:19
  • @Martin: i deleted that sentence for being "too generally" - but i think we agree, that in "most cases", by wich i mean "for defining just a simple function that could be done exactly the same using `function(){}`-syntax", the `new Function`-syntax _is_ a bad idea. i agree with you that in _special cases_ it's a really great possibility. – oezi Jan 20 '11 at 09:23
  • @oezi: we agree absolutely :) – Martin Jespersen Jan 20 '11 at 09:25
  • @Dan my firebug what deactivated while running your benchmark, i don't think it affects the results this way. is's a fact tha `new Function` is slower than normal function-syntax, if it#s now for you, tehres an eror in the benchmark. || @Martin: i edited my post trying to give some examples for "good" use of `new Function` - maybe you have any more suggestions. – oezi Jan 20 '11 at 09:39
  • @Dan maybe you shoudl take a look at Felix Klings benchmark here: http://jsperf.com/function-vs-function/2 - this seems to be more clearly than yours to show wich possibility performs faster. – oezi Jan 20 '11 at 09:42
  • If I create a `new Function` dynamically once, and call that function a million times, wouldn't the function itself run just as fast as a "hardcoded" function? Only eval'ing the string to executable code should be slow, but not the code itself (you should obviously not interpret the same string in a loop everytime). – CodeManX Apr 13 '15 at 11:50