Iterative iteration of JavaScript performance optimization (DUFF)

Source: Internet
Author: User

This is the time to learn the "high-performance JavaScript" This book, here to record some learning experience and notes ...

The loop iteration of "Duff's Device" optimizes the number of iterations over 1000 cycles.

The functions are as follows:

1 functionDuff (items) {2     if(Items &&items.length) {3         varL = items.length, i = l% 8;4          while(i) {5Console.log (items[--l]);6I--;7         }8i = Math.floor (ITEMS.LENGTH/8);9          while(i) {TenConsole.log (items[--l]); OneConsole.log (items[--l]); AConsole.log (items[--l]); -Console.log (items[--l]); -Console.log (items[--l]); theConsole.log (items[--l]); -Console.log (items[--l]); -Console.log (items[--l]); -I--; +         } -     } + } A  atDuff ([1,2,3,4,5,6,7,8,9,10]);

After function execution, console output: 10 9 8 7 6 5 4 3 2 1

The number of iterations is less than 1000, with little performance improvement. However, more than 1000 times the efficiency of Deff function is significantly improved.

Iterative iteration of JavaScript performance optimization (DUFF)

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.