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();
24 jQuery("#times").html(times);
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[input:checked] + td.test').benchmark();
39 jQuery("button.retryTies").bind("click", function() { jQuery("tr[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) {
56 return "<th>" + i + "</th>"
59 jQuery("thead tr").append(headers);
62 for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
63 footers += "<th></th>"
66 for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
67 wlfooters += "<td><span class='wins'>W</span> / <span class='fails'>F</span></th>"
69 jQuery("tfoot tr:first").append(footers);
70 jQuery("tfoot tr:last").append(wlfooters);
74 benchmark = function(list, times, libraries) {
76 var times = times || 50;
78 var code = jQuery(el).text().replace(/^-/, "");
79 if(!libraries[0].match(/^jQ/)) {
80 code = code.replace(/@/, "");
83 for(i = 0; i < times + 2; i++) {
86 window[libraries[0]](code);
88 timeArr.push(new Date() - time);
90 var diff = Math.sum(timeArr) - Math.max.apply( Math, timeArr )
91 - Math.min.apply( Math, timeArr );
93 var libRes = window[libraries[0]](code);
94 var jqRes = jQuery(code);
95 if(((jqRes.length == 0) && (libRes.length != 0)) ||
96 (libRes.length > 0 && (jqRes.length == libRes.length)) ||
97 ((libraries[0] == "cssQuery" || libraries[0] == "jQuery") && code.match(/nth\-child/) && (libRes.length > 0)) ||
98 ((libraries[0] == "jQold") && jqRes.length > 0)) {
99 jQuery(el).parent().append("<td>" + Math.round(diff / times * 100) / 100 + "ms</td>");
101 jQuery(el).parent().append("<td class='fail'>FAIL</td>");
104 jQuery(el).parent().append("<td class='fail'>FAIL</td>");
106 setTimeout(benchmarkList(list, times, libraries), 100);
107 } else if(libraries[1]) {
108 benchmark(jQuery.benchmarker.startingList, times, libraries.slice(1));
110 jQuery("tbody tr").each(function() {
111 var winners = jQuery("td:gt(1)", this).min(2);
112 if(winners.length == 1) winners.addClass("winner");
113 else winners.addClass("tie");
115 setTimeout(count, 100);
119 function benchmarkList(list, times, libraries) {
121 benchmark(list.slice(1), times, libraries);
126 for(i = 3; i <= jQuery.benchmarker.libraries.length + 2 ; i++) {
127 var fails = jQuery("td:nth-child(" + i + ").fail").length;
128 var wins = jQuery("td:nth-child(" + i + ").winner").length;
129 jQuery("tfoot tr:first th:eq(" + (i - 1) + ")")
130 .html("<span class='wins'>" + wins + "</span> / <span class='fails'>" + fails + "</span>");
135 jQuery.fn.maxmin = function(tolerance, maxmin, percentage) {
136 tolerance = tolerance || 0;
137 var target = Math[maxmin].apply(Math, jQuery.map(this, function(i) {
138 var parsedNum = parseFloat(i.innerHTML.replace(/[^\.\d]/g, ""));
139 if(parsedNum || (parsedNum == 0)) return parsedNum;
141 return this.filter(function() {
142 if( withinTolerance(parseFloat(this.innerHTML.replace(/[^\.\d]/g, "")), target, tolerance, percentage) ) return true;
146 jQuery.fn.max = function(tolerance, percentage) { return this.maxmin(tolerance, "max", percentage) }
147 jQuery.fn.min = function(tolerance, percentage) { return this.maxmin(tolerance, "min", percentage) }
149 function withinTolerance(number, target, tolerance, percentage) {
150 if(percentage) { var high = target + ((tolerance / 100) * target); var low = target - ((tolerance / 100) * target); }
151 else { var high = target + tolerance; var low = target - tolerance; }
152 if(number >= low && number <= high) return true;
155 Math.sum = function(arr) {
157 for(i = 0; i < arr.length; i++) sum += arr[i];