Fixed the build/speed testing application to work with current versions of jQuery...
[jquery.git] / build / speed / benchmarker.js
1   jQuery.benchmarker.tests = [
2                 "*",
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]"
15                 ]
16
17   jQuery.fn.benchmark = function() {
18     this.each(function() {
19       try {
20         jQuery(this).parent().children("*:gt(1)").remove();
21       } catch(e) { }
22     })
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);
27   }
28
29   jQuery(function() {
30     for(i = 0; i < jQuery.benchmarker.tests.length; i++) {
31       jQuery("tbody").append("<tr><td class='test'>" + jQuery.benchmarker.tests[i] + "</td></tr>");
32     }
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();
37     });
38
39     jQuery("button.retryTies").bind("click", function() { jQuery("tr:has(td.tie) td.test").benchmark() })
40
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 }) })
43
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(); })
52       })
53     })
54
55     var headers = jQuery.map(jQuery.benchmarker.libraries, function(i,n) {
56       var extra = n == 0 ? "basis - " : "";
57       return "<th>" + extra + i + "</th>"
58     }).join("");
59
60     jQuery("thead tr").append(headers);
61
62     var footers = "";
63     for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
64       footers += "<th></th>"
65
66     var wlfooters = "";
67     for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
68       wlfooters += "<td><span class='wins'>W</span> / <span class='fails'>F</span></th>"
69
70     jQuery("tfoot tr:first").append(footers);
71     jQuery("tfoot tr:last").append(wlfooters);
72
73   });
74
75    benchmark = function(list, times, libraries) {
76      if(list[0]) {
77        var times = times || 50;
78        var el = list[0];
79        var code = jQuery(el).text().replace(/^-/, "");
80          if(!libraries[0].match(/^jQ/)) {
81            code = code.replace(/@/, "");
82          }
83          var timeArr = []
84          for(i = 0; i < times + 2; i++) {
85            var time = new Date()
86            try {
87              window[libraries[0]](code);
88            } catch(e) { }
89            timeArr.push(new Date() - time);
90          }
91          var diff = Math.sum(timeArr) - Math.max.apply( Math, timeArr )
92                 - Math.min.apply( Math, timeArr );
93          try {
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>");
101            } else {
102              jQuery(el).parent().append("<td class='fail'>FAIL</td>");
103            }
104          } catch(e) {
105            jQuery(el).parent().append("<td class='fail'>FAIL</td>");
106          }
107        setTimeout(benchmarkList(list, times, libraries), 100);
108      } else if(libraries[1]) {
109        benchmark(jQuery.benchmarker.startingList, times, libraries.slice(1));
110      } else {
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");
115        });
116        setTimeout(count, 100);
117      }
118    }
119
120   function benchmarkList(list, times, libraries) {
121     return function() {
122       benchmark(list.slice(1), times, libraries);
123     }
124   }
125
126  function count() {
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>");
132    }
133  }
134
135
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;
141    }));
142    return this.filter(function() {
143      if( withinTolerance(parseFloat(this.innerHTML.replace(/[^\.\d]/g, "")), target, tolerance, percentage) ) return true;
144    })
145  }
146
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) }
149
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;
154  }
155
156  Math.sum = function(arr) {
157    var sum = 0;
158    for(i = 0; i < arr.length; i++) sum += arr[i];
159    return sum;
160  }