1 jQuery.benchmarker.tests = [
3 "body", "body div", "div",
4 "div div div", "div div", ".dialog", "div.dialog", "div .dialog",
5 "#speech5", "div#speech5", "div #speech5", "div > div", "div.scene div.dialog",
6 "div#scene1.scene div.dialog div", "#scene1 #speech1", "body > div.dialog div#speech5",
7 "div:nth-child(even)", "div:nth-child(odd)",
8 "div:nth-child(1)", "div:nth-child(2n)",
9 "div:nth-child(2n+3)", "div:first-child",
10 "div:last-child", "div:only-child",
11 "div:contains(CELIA)",
12 "div ~ div", "div + div",
13 "div[@class]", "div[@class=dialog]", "div[@class!=dialog]",
14 "div[@class^=dialog]", "div[@class$=dialog]", "div[@class*=dialog]"
17 jQuery.fn.benchmark = function() {
18 this.each(function() {
20 jQuery(this).parent().children("*:gt(1)").remove();
23 // set # times to run the test in index.html
24 var times = parseInt(jQuery("#times").val());
25 jQuery.benchmarker.startingList = this.get();
26 benchmark(this.get(), times, jQuery.benchmarker.libraries);
30 for(i = 0; i < jQuery.benchmarker.tests.length; i++) {
31 jQuery("tbody").append("<tr><td class='test'>" + jQuery.benchmarker.tests[i] + "</td></tr>");
33 jQuery("tbody tr:first-child").remove();
34 jQuery("td.test").before("<td><input type='checkbox' checked='checked' /></td>");
35 jQuery("button.runTests").bind("click", function() {
36 jQuery('td:has(input:checked) + td.test').benchmark();
39 jQuery("button.retryTies").bind("click", function() { jQuery("tr:has(td.tie) td.test").benchmark() })
41 jQuery("button.selectAll").bind("click", function() { jQuery("input[@type=checkbox]").each(function() { this.checked = true }) })
42 jQuery("button.deselectAll").bind("click", function() { jQuery("input[@type=checkbox]").each(function() { this.checked = false }) })
44 jQuery("#addTest").bind("click", function() {
45 jQuery("table").append("<tr><td><input type='checkbox' /></td><td><input type='text' /><button>Add</button></td></tr>");
46 jQuery("div#time-test > button").each(function() { this.disabled = true; })
47 jQuery("tbody tr:last button").bind("click", function() {
48 var td = jQuery(this).parent();
49 td.html("<button>-</button>" + jQuery(this).prev().val()).addClass("test");
50 jQuery("div#time-test > button").each(function() { this.disabled = false; })
51 jQuery("button", td).bind("click", function() { jQuery(this).parents("tr").remove(); })
55 var headers = jQuery.map(jQuery.benchmarker.libraries, function(i,n) {
56 var extra = n == 0 ? "basis - " : "";
57 return "<th>" + extra + i + "</th>"
60 jQuery("thead tr").append(headers);
63 for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
64 footers += "<th></th>"
67 for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
68 wlfooters += "<td><span class='wins'>W</span> / <span class='fails'>F</span></th>"
70 jQuery("tfoot tr:first").append(footers);
71 jQuery("tfoot tr:last").append(wlfooters);
75 benchmark = function(list, times, libraries) {
77 var times = times || 50;
79 var code = jQuery(el).text().replace(/^-/, "");
80 if(!libraries[0].match(/^jQ/)) {
81 code = code.replace(/@/, "");
84 for(i = 0; i < times + 2; i++) {
87 window[libraries[0]](code);
89 timeArr.push(new Date() - time);
91 var diff = Math.sum(timeArr) - Math.max.apply( Math, timeArr )
92 - Math.min.apply( Math, timeArr );
94 var libRes = window[libraries[0]](code);
95 var jqRes = jQuery(code);
96 if(((jqRes.length == 0) && (libRes.length != 0)) ||
97 (libRes.length > 0 && (jqRes.length == libRes.length)) ||
98 ((libraries[0] == "cssQuery" || libraries[0] == "jQuery") && code.match(/nth\-child/) && (libRes.length > 0)) ||
99 ((libraries[0] == "jQold") && jqRes.length > 0)) {
100 jQuery(el).parent().append("<td>" + Math.round(diff / times * 100) / 100 + "ms</td>");
102 jQuery(el).parent().append("<td class='fail'>FAIL</td>");
105 jQuery(el).parent().append("<td class='fail'>FAIL</td>");
107 setTimeout(benchmarkList(list, times, libraries), 100);
108 } else if(libraries[1]) {
109 benchmark(jQuery.benchmarker.startingList, times, libraries.slice(1));
111 jQuery("tbody tr").each(function() {
112 var winners = jQuery("td:gt(1)", this).min(2);
113 if(winners.length == 1) winners.addClass("winner");
114 else winners.addClass("tie");
116 setTimeout(count, 100);
120 function benchmarkList(list, times, libraries) {
122 benchmark(list.slice(1), times, libraries);
127 for(i = 3; i <= jQuery.benchmarker.libraries.length + 2 ; i++) {
128 var fails = jQuery("td:nth-child(" + i + ").fail").length;
129 var wins = jQuery("td:nth-child(" + i + ").winner").length;
130 jQuery("tfoot tr:first th:eq(" + (i - 1) + ")")
131 .html("<span class='wins'>" + wins + "</span> / <span class='fails'>" + fails + "</span>");
136 jQuery.fn.maxmin = function(tolerance, maxmin, percentage) {
137 tolerance = tolerance || 0;
138 var target = Math[maxmin].apply(Math, jQuery.map(this, function(i) {
139 var parsedNum = parseFloat(i.innerHTML.replace(/[^\.\d]/g, ""));
140 if(parsedNum || (parsedNum == 0)) return parsedNum;
142 return this.filter(function() {
143 if( withinTolerance(parseFloat(this.innerHTML.replace(/[^\.\d]/g, "")), target, tolerance, percentage) ) return true;
147 jQuery.fn.max = function(tolerance, percentage) { return this.maxmin(tolerance, "max", percentage) }
148 jQuery.fn.min = function(tolerance, percentage) { return this.maxmin(tolerance, "min", percentage) }
150 function withinTolerance(number, target, tolerance, percentage) {
151 if(percentage) { var high = target + ((tolerance / 100) * target); var low = target - ((tolerance / 100) * target); }
152 else { var high = target + tolerance; var low = target - tolerance; }
153 if(number >= low && number <= high) return true;
156 Math.sum = function(arr) {
158 for(i = 0; i < arr.length; i++) sum += arr[i];