This article will introduce the Efficiency Test of nested javascript function calls. I hope you can understand the performance of repeated function calls and optimize it to avoid it.
The nested function definition in Javascript brings a lot of convenience to our development, but how is its 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 is executed in nodejs and the result is as follows:
# Node test2.js
Start1: 190 ms
Start2: 78 ms
Start3: 58 ms
Start4: 11 ms
If a function is frequently called, there will be some efficiency problems.