Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript function vs new Function

Tags:

javascript

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

like image 721
Dan Avatar asked Feb 26 '23 08:02

Dan


1 Answers

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

like image 184
Felix Kling Avatar answered Mar 08 '23 05:03

Felix Kling