Posted By

irrevokable on 04/30/10


Tagged

javascript test profile as2 benchmark


Versions (?)

JS/AS2 simple Benchmark Tool


 / Published in: JavaScript
 

Just a simple benchmark utility, working both in AS2 & JS.

  1. // IRR Bencher
  2. /**
  3.   * @param f function to test
  4.   * @param i iterations count (default is 10000)
  5.   * @param n number of tests (default is 10)
  6.   * @param a args
  7.   * @param l label -- not implemented yet
  8.  */
  9. function Bencher(f, i, n, a, l){
  10. i = i||10000;
  11. n = n||10;
  12. l = l||'#test#';
  13. var e=-1, k=-1, t=0;
  14. var ar=[];
  15.  
  16. while(++e<n){
  17. k = -1;
  18. t = getTimer();
  19. while(++k<i){
  20. a ? f.apply(null,a): f();
  21. }
  22. ar[e] = getTimer() - t;
  23. }
  24. return avg(ar)+' ms ';
  25. }
  26.  
  27. //check DOM Existance
  28. if(document){
  29. var trace = function(v){ document.writeln(v+'<br>')};
  30. var getTimer = function(){
  31. var _time = new Date();
  32. return parseFloat(_time.valueOf());
  33. }
  34. }
  35. // count average
  36. function avg(a){
  37. var s = 0;
  38. for(var i in a)s+=a[i];
  39. return s/a.length;
  40. }
  41.  
  42.  
  43. // SIMPLE TESTS
  44. var bms =
  45. [
  46. Bencher(function(){var a=[];}),
  47. Bencher(function(){var a={};}),
  48. Bencher(function(){var a=new Array;}),
  49. Bencher(function(){var a=new Object;}),
  50. Bencher(function(){var a=Math.random();})
  51. ];
  52. trace(bms);
  53.  
  54.  
  55.  
  56. // HTML CODE
  57. /*
  58. <html>
  59. <head>
  60. <script src="Bencher-jsas.js"></script>
  61. </head>
  62. <body>
  63. </body>
  64. </html>
  65. */

Report this snippet  

You need to login to post a comment.