Test the efficiency of nested javascript function calls

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.