benchmarker.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. jQuery.benchmarker.tests = [
  2. // Selectors from:
  3. // http://ejohn.org/blog/selectors-that-people-actually-use/
  4. /*
  5. // For Amazon.com
  6. "#navAmazonLogo", "#navSwmSkedPop",
  7. ".navbar", ".navGreeting",
  8. "div", "table",
  9. "img.navCrossshopTabCap", "span.navGreeting",
  10. "#navbar table", "#navidWelcomeMsg span",
  11. "div#navbar", "ul#navAmazonLogo",
  12. "#navAmazonLogo .navAmazonLogoGatewayPanel", "#navidWelcomeMsg .navGreeting",
  13. ".navbar .navAmazonLogoGatewayPanel", ".navbar .navGreeting",
  14. "*",
  15. "#navAmazonLogo li.navAmazonLogoGatewayPanel", "#navidWelcomeMsg span.navGreeting",
  16. "a[name=top]", "form[name=site-search]",
  17. ".navbar li", ".navbar span",
  18. "[name=top]", "[name=site-search]",
  19. "ul li", "a img",
  20. "#navbar #navidWelcomeMsg", "#navbar #navSwmDWPop",
  21. "#navbar ul li", "#navbar a img"
  22. */
  23. // For Yahoo.com
  24. "#page", "#masthead", "#mastheadhd",
  25. ".mastheadbd", ".first", ".on",
  26. "div", "li", "a",
  27. "div.mastheadbd", "li.first", "li.on",
  28. "#page div", "#dtba span",
  29. "div#page", "div#masthead",
  30. "#page .mastheadbd", "#page .first",
  31. ".outer_search_container .search_container", ".searchbox_container .inputtext",
  32. "*",
  33. "#page div.mastheadbd", "#page li.first",
  34. "input[name=p]", "a[name=marketplace]",
  35. ".outer_search_container div", ".searchbox_container span",
  36. "[name=p]", "[name=marketplace]",
  37. "ul li", "form input",
  38. "#page #e2econtent", "#page #e2e"
  39. ];
  40. jQuery.fn.benchmark = function() {
  41. this.each(function() {
  42. try {
  43. jQuery(this).parent().children("*:gt(1)").remove();
  44. } catch(e) { }
  45. })
  46. // set # times to run the test in index.html
  47. var times = parseInt(jQuery("#times").val());
  48. jQuery.benchmarker.startingList = this.get();
  49. benchmark(this.get(), times, jQuery.benchmarker.libraries);
  50. }
  51. jQuery(function() {
  52. for(i = 0; i < jQuery.benchmarker.tests.length; i++) {
  53. jQuery("tbody").append("<tr><td class='test'>" + jQuery.benchmarker.tests[i] + "</td></tr>");
  54. }
  55. jQuery("tbody tr:first-child").remove();
  56. jQuery("td.test").before("<td><input type='checkbox' checked='checked' /></td>");
  57. jQuery("button.runTests").bind("click", function() {
  58. jQuery('td:has(input:checked) + td.test').benchmark();
  59. });
  60. jQuery("button.retryTies").bind("click", function() { jQuery("tr:has(td.tie) td.test").benchmark() })
  61. jQuery("button.selectAll").bind("click", function() { jQuery("input[type=checkbox]").each(function() { this.checked = true }) })
  62. jQuery("button.deselectAll").bind("click", function() { jQuery("input[type=checkbox]").each(function() { this.checked = false }) })
  63. jQuery("#addTest").bind("click", function() {
  64. jQuery("table").append("<tr><td><input type='checkbox' /></td><td><input type='text' /><button>Add</button></td></tr>");
  65. jQuery("div#time-test > button").each(function() { this.disabled = true; })
  66. jQuery("tbody tr:last button").bind("click", function() {
  67. var td = jQuery(this).parent();
  68. td.html("<button>-</button>" + jQuery(this).prev().val()).addClass("test");
  69. jQuery("div#time-test > button").each(function() { this.disabled = false; })
  70. jQuery("button", td).bind("click", function() { jQuery(this).parents("tr").remove(); })
  71. })
  72. })
  73. var headers = jQuery.map(jQuery.benchmarker.libraries, function(i,n) {
  74. var extra = n == 0 ? "basis - " : "";
  75. return "<th>" + extra + i + "</th>"
  76. }).join("");
  77. jQuery("thead tr").append(headers);
  78. var footers = "";
  79. for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
  80. footers += "<th></th>"
  81. var wlfooters = "";
  82. for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
  83. wlfooters += "<td><span class='wins'>W</span> / <span class='fails'>F</span></th>"
  84. jQuery("tfoot tr:first").append(footers);
  85. jQuery("tfoot tr:last").append(wlfooters);
  86. });
  87. benchmark = function(list, times, libraries) {
  88. if(list[0]) {
  89. var times = times || 50;
  90. var el = list[0];
  91. var code = jQuery(el).text().replace(/^-/, "");
  92. var timeArr = []
  93. for(i = 0; i < times + 2; i++) {
  94. var time = new Date()
  95. try {
  96. window[libraries[0]](code);
  97. } catch(e) { }
  98. timeArr.push(new Date() - time);
  99. }
  100. var diff = Math.sum(timeArr) - Math.max.apply( Math, timeArr )
  101. - Math.min.apply( Math, timeArr );
  102. try {
  103. var libRes = window[libraries[0]](code);
  104. var jqRes = jQuery(code);
  105. if(((jqRes.length == 0) && (libRes.length != 0)) ||
  106. (libRes.length > 0 && (jqRes.length == libRes.length)) ||
  107. ((libraries[0] == "cssQuery" || libraries[0] == "jQuery") && code.match(/nth\-child/) && (libRes.length > 0)) ||
  108. ((libraries[0] == "jQold") && jqRes.length > 0)) {
  109. jQuery(el).parent().append("<td>" + Math.round(diff / times * 100) / 100 + "ms</td>");
  110. } else {
  111. jQuery(el).parent().append("<td class='fail'>FAIL</td>");
  112. }
  113. } catch(e) {
  114. jQuery(el).parent().append("<td class='fail'>FAIL</td>");
  115. }
  116. setTimeout(benchmarkList(list, times, libraries), 100);
  117. } else if(libraries[1]) {
  118. benchmark(jQuery.benchmarker.startingList, times, libraries.slice(1));
  119. } else {
  120. jQuery("tbody tr").each(function() {
  121. var winners = jQuery("td:gt(1)", this).min(2);
  122. if(winners.length == 1) winners.addClass("winner");
  123. else winners.addClass("tie");
  124. });
  125. setTimeout(count, 100);
  126. }
  127. }
  128. function benchmarkList(list, times, libraries) {
  129. return function() {
  130. benchmark(list.slice(1), times, libraries);
  131. }
  132. }
  133. function count() {
  134. for(i = 3; i <= jQuery.benchmarker.libraries.length + 2 ; i++) {
  135. var fails = jQuery("td:nth-child(" + i + ").fail").length;
  136. var wins = jQuery("td:nth-child(" + i + ").winner").length;
  137. jQuery("tfoot tr:first th:eq(" + (i - 1) + ")")
  138. .html("<span class='wins'>" + wins + "</span> / <span class='fails'>" + fails + "</span>");
  139. }
  140. }
  141. jQuery.fn.maxmin = function(tolerance, maxmin, percentage) {
  142. tolerance = tolerance || 0;
  143. var target = Math[maxmin].apply(Math, jQuery.map(this, function(i) {
  144. var parsedNum = parseFloat(i.innerHTML.replace(/[^\.\d]/g, ""));
  145. if(parsedNum || (parsedNum == 0)) return parsedNum;
  146. }));
  147. return this.filter(function() {
  148. if( withinTolerance(parseFloat(this.innerHTML.replace(/[^\.\d]/g, "")), target, tolerance, percentage) ) return true;
  149. })
  150. }
  151. jQuery.fn.max = function(tolerance, percentage) { return this.maxmin(tolerance, "max", percentage) }
  152. jQuery.fn.min = function(tolerance, percentage) { return this.maxmin(tolerance, "min", percentage) }
  153. function withinTolerance(number, target, tolerance, percentage) {
  154. if(percentage) { var high = target + ((tolerance / 100) * target); var low = target - ((tolerance / 100) * target); }
  155. else { var high = target + tolerance; var low = target - tolerance; }
  156. if(number >= low && number <= high) return true;
  157. }
  158. Math.sum = function(arr) {
  159. var sum = 0;
  160. for(i = 0; i < arr.length; i++) sum += arr[i];
  161. return sum;
  162. }