tailieunhanh - Phát triển Javascript - part 10

Tham khảo tài liệu 'phát triển javascript - part 10', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Performance Tests 63 Run tests runBenchmark for-loop forLoop runBenchmark for-loop cached length forLoopCachedLength runBenchmark for-loop direct array access forLoopDirectAccess runBenchmark while-loop whileLoop runBenchmark while-loop cached length property whileLoopCachedLength runBenchmark reversed while-loop reversedWhileLoop runBenchmark double reversed while-loop doubleReversedWhileLoop The setTimeout call is important to avoid choking the browser while testing. The browser uses a single thread to run JavaScript fire events and render web pages and the timers allow the browser some breathing room to pick up on queued tasks between tests that are potentially long running. Breaking the workload up with timers also avoids browsers interrupting the tests to warn us about slow scripts. To run these benchmarks all we need is a simple HTML file like the one in Listing that loads the script. Save the file in benchmarks . Listing YUI Test HTML fixture file DOCTYPE HTML PUBLIC - W3C DTD HTML EN http TR html4 html head title Relative performance of loops title meta http-equiv content-type content text html charset UTF-8 head body h1 Relative performance of loops h1 script type text javascript src . lib script script type text javascript src script body html All the tests do the exact same thing loop over all items in the array and access the current item. Accessing the current item adds to the footprint of the test but it also allows us to compare the loop that accesses the current item in the loop Download from 64 Test to Learn conditional with the rest. This is not always a safe choice because empty strings null 0 and other false values will terminate the loop. Also this style of looping performs terribly on some browsers and should be avoided. Because all the tests access the current item we can disregard the overhead as fluctuations in the test results will be the result of the .

TỪ KHÓA LIÊN QUAN