fun_perf.html 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <html>
  2. <head>
  3. <title>clocking fun</title>
  4. <style type="text/css">
  5. @import "../resources/dojo.css";
  6. </style>
  7. <script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug:true"></script>
  8. <script type="text/javascript" src="../functional.js"></script>
  9. <script type="text/javascript">
  10. dojo.addOnLoad(function(){
  11. var LEN = 1000, ITER = 100, SUM = (LEN - 1) * LEN / 2;
  12. var foldl_1 = function(/*Array*/ a, /*Function*/ f, /*Object*/ z){
  13. for(var i = 0; i < a.length; z = f.call(dojo.global, z, a[i++]));
  14. return z;
  15. };
  16. var foldl_2 = function(/*Array*/ a, /*Function*/ f, /*Object*/ z){
  17. dojo.forEach(a, function(x){ z = f.call(dojo.global, z, x); });
  18. return z;
  19. };
  20. var foldl_3 = function(/*Array*/ a, /*Function*/ f, /*Object*/ z){
  21. a.forEach(function(x){ z = f.call(dojo.global, z, x); });
  22. return z;
  23. };
  24. var sample = dojox.lang.functional.repeat(LEN, function(x){ return x + 1; }, 0);
  25. console.profile("dojox.lang.functional.foldl");
  26. for(var i = 0; i < ITER; ++i){
  27. var t = dojox.lang.functional.foldl(sample, function(a, b){ return a + b; }, 0);
  28. console.assert(t == SUM);
  29. }
  30. console.profileEnd("dojox.lang.functional.foldl");
  31. console.profile("dojox.lang.functional.reduce");
  32. for(var i = 0; i < ITER; ++i){
  33. var t = dojox.lang.functional.reduce(sample, function(a, b){ return a + b; });
  34. console.assert(t == SUM);
  35. }
  36. console.profileEnd("dojox.lang.functional.reduce");
  37. console.profile("raw loop");
  38. for(var i = 0; i < ITER; ++i){
  39. var t = foldl_1(sample, function(a, b){ return a + b; }, 0);
  40. console.assert(t == SUM);
  41. }
  42. console.profileEnd("raw loop");
  43. console.profile("dojo.forEach");
  44. for(var i = 0; i < ITER; ++i){
  45. var t = foldl_2(sample, function(a, b){ return a + b; }, 0);
  46. console.assert(t == SUM);
  47. }
  48. console.profileEnd("dojo.forEach");
  49. console.profile("Array.forEach");
  50. for(var i = 0; i < ITER; ++i){
  51. var t = foldl_3(sample, function(a, b){ return a + b; }, 0);
  52. console.assert(t == SUM);
  53. }
  54. console.profileEnd("Array.forEach");
  55. });
  56. </script>
  57. </head>
  58. <body>
  59. <p>This test is meant to run on Firefox with Firebug installed.</p>
  60. </body>
  61. </html>