4

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

3
  • 4
    Flawed micro benchmark is flawed. Aside from that, you're mainly testing for the performance of console.log here anyways :) Commented Jan 20, 2011 at 9:09
  • You are quite right!!!!! Commented Jan 20, 2011 at 9:10
  • On my computer it runs within +/- 3 percent, alternating. A few times I've obtained the exact same times for both scenarios. Commented Oct 10, 2013 at 14:52

2 Answers 2

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

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.

8 Comments

but new Function runs even faster! Do you also see what I see?
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.
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: 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: we agree absolutely :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.