`new function()` with lower case "f" in JavaScript
My intuition says using the new keyword would be slower. Is there any noticeable benefit to using either method?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
My guess would be that the function constructor form (
new function() { }) would be faster than returning an object literal in a closure ((function(){ return {}; })()) because the latter seems to be doing a little more work than the former.However, it appears I am wrong, at least for a couple modern JavaScript engines. This jsPerf comparison shows the literal/closure form to be considerably faster in both Chrome and Firefox.
Ultimately, I think the correctness of the code and the clarity of the intent of the programmer is more important than such a trivial optimization (which likely varies greatly between real-world JavaScript engines anyway).