The definition of functional nesting in JavaScript has brought a lot of convenience to our development, but what about his efficiency? I did a simple test.
The code is as follows |
Copy Code |
Test
function Func1 (A, b) { Return (function (A, b) { Return (function (A, b) {return a+b}) (); })(); }
function Func2 (A, B) { var nest1 = function (A, B) { var nest11 = function () { return a+b; } Return nest11 (A+B); } Return Nest1 (A,B); }
function Func3 (A, b) { function Nest1 (A, b) { function Nest11 (A, b) { return a+b; } Return Nest11 (A, b); }
Return Nest1 (A, b); }
function Nest44 (A, b) { return a+b; }
function Nest4 (A, b) { Return Nest44 (A, b); }
function Func4 (A, b) { Return Nest4 (A, b); }
Console.time ("Start1"); for (var i=0; i<1000000; i++) { Func1 (1,1); } Console.timeend ("Start1");
Console.time ("Start2"); for (var i=0; i<1000000; i++) { Func2 (1,1); } Console.timeend ("Start2");
Console.time ("Start3"); for (var i=0; i<1000000; i++) { FUNC3 (1,1); } Console.timeend ("Start3");
Console.time ("Start4"); for (var i=0; i<1000000; i++) { Func4 (1,1); } Console.timeend ("Start4"); |
This code looks at execution with Nodejs and gets the following results:
# node Test2.js
Start1:190ms
Start2:78ms
Start3:58ms
Start4:11ms
If a function is called frequently, there will be some efficiency problems.