この記事では、JSの実行時間を単純にテストする方法について説明します。次のように、参照のために共有してください。
<!doctype html> <html lang = "zh-cn"> <head> <meta charset = "utf-8"> <title> jsテストループrup rup </title> <scrip> var arr = []; var max = 10000000; // window.addeventlistener( "load"、function(){settimeout(){// aririze aririze for(var i = 0; i <max; i ++){arr [i] = i+1;} //すべてのボタンdocument.getelementbyid( "div1")。 = "なし"; // 1)ループ機能Test1(){var d1 = new date(); var sum = 0; for(var i = 0; i <arr.length; i ++){sum+= arr [i]} var d2 = new date(); var x = d2 -d1; console.log( "計算結果:" + sum + "、time:" + x); } // 2)for..in loop function test2(){var d1 = new date(); var sum = 0; for(var i in arr){sum += arr [i]} var d2 = new date(); var x = d2 -d1; console.log( "for..in計算結果:" + sum + "、time:" + x); } // 3)ループ関数test3(){var d1 = new date(); var sum = 0; arr.foreach(function(n){sum += n;})var d2 = new date(); var x = d2 -d1; console.log( "foreach計算結果:" + sum + "、time:" + x); } </script> </head> <body> f12を押してコントローラー出力を表示してください<br/> <div id = "div1" style = "display:none;"> <input type = "button" value = "onclick =" test1(); " /> <br /> <input type = "button" value = "for..in loop" onclick = "test2();" /> <br /> <input type = "button" value = "foreach loop" onclick = "test3();"の使用/> <br/> <br/> </div> <div id = "div2">初期化... </div> </body> </html>複製画像:
For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript Traversal Algorithm and Skills", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Errors and Debugging Skills", "Summary of JavaScript Data構造とアルゴリズムのスキル」および「JavaScriptの数学的操作の使用の要約」
この記事がみんなのJavaScriptプログラミングに役立つことを願っています。