peller.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <html>
  2. <head>
  3. <title>peller's test</title>
  4. <script type="text/javascript" src="../../../dojo/dojo.js"></script>
  5. <script type="text/javascript">
  6. var lq = [];
  7. function log(s) {
  8. lq.push(s);
  9. //console.log(s);
  10. }
  11. function dumpLog() {
  12. dojo.forEach(lq, function(l) { console.log(l); });
  13. lq = [];
  14. }
  15. dojo.addOnLoad(function() {
  16. forLoop();
  17. forEachLoop();
  18. forAgain();
  19. forEachAgain();
  20. dumpLog();
  21. });
  22. function forLoop() {
  23. var x=0;
  24. var a = g_a;
  25. var start=new Date();
  26. for(var i=0;i<100000;i++){x=x+a[i];};
  27. log("for loop elapsed:"+(new Date()-start)+" value="+x);
  28. }
  29. function forEachLoop() {
  30. var x=0;
  31. var a = g_a;
  32. var start=new Date();
  33. dojo.forEach(a, function(v,i){x=x+a[i];});
  34. log("dojo.forEach elapsed:"+(new Date()-start)+" value="+x);
  35. }
  36. function forAgain(){
  37. log("for results:");
  38. var start=new Date();
  39. var x=0;
  40. for(var i=0;i<100000;i++){x=x+g_a[i];}
  41. log("elapsed:"+(new Date()-start)+" value="+x);
  42. }
  43. function forEachAgain(){
  44. log("forEach results:");
  45. var a = g_a;
  46. var x=0;
  47. var start=new Date();
  48. a.forEach(function(v,i){x=x+a[i];});
  49. log("elapsed:"+(new Date()-start)+" value="+x);
  50. }
  51. var g_a = new Array(100000);
  52. for(var i=0; i<100000;i++){g_a[i]=i;}
  53. var start, x, i;
  54. log("inline for results:");
  55. start=new Date();
  56. x=0;
  57. for(i=0;i<100000;i++){x=x+g_a[i];}
  58. log("elapsed:"+(new Date()-start)+" value="+x);
  59. log("inline forEach results:");
  60. start=new Date();
  61. x=0;
  62. g_a.forEach(function(v,i){x=x+g_a[i];});
  63. log("elapsed:"+(new Date()-start)+" value="+x);
  64. dumpLog();
  65. </script>
  66. </head>
  67. <body>
  68. </body>
  69. </html>