Added the Speed Test Suite. Can be made by doing 'make speed'.
[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     var times = 5;
24     jQuery("#times").html(times);
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[input:checked] + td.test').benchmark();
37     });
38
39     jQuery("button.retryTies").bind("click", function() { jQuery("tr[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) {
56       return "<th>" + i + "</th>"
57     }).join("");
58
59     jQuery("thead tr").append(headers);
60
61     var footers = "";
62     for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
63       footers += "<th></th>"
64
65     var wlfooters = "";
66     for(i = 0; i < jQuery.benchmarker.libraries.length; i++)
67       wlfooters += "<td><span class='wins'>W</span> / <span class='fails'>F</span></th>"
68
69     jQuery("tfoot tr:first").append(footers);
70     jQuery("tfoot tr:last").append(wlfooters);
71
72   });
73
74    benchmark = function(list, times, libraries) {
75      if(list[0]) {
76        var times = times || 50;
77        var el = list[0];
78        var code = jQuery(el).text().replace(/^-/, "");
79          if(!libraries[0].match(/^jQ/)) {
80            code = code.replace(/@/, "");
81          }
82          var timeArr = []
83          for(i = 0; i < times + 2; i++) {
84            var time = new Date()
85            try {
86              window[libraries[0]](code);
87            } catch(e) { }
88            timeArr.push(new Date() - time);
89          }
90          var diff = Math.sum(timeArr) - Math.max.apply( Math, timeArr )
91                 - Math.min.apply( Math, timeArr );
92          try {
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>");
100            } else {
101              jQuery(el).parent().append("<td class='fail'>FAIL</td>");
102            }
103          } catch(e) {
104            jQuery(el).parent().append("<td class='fail'>FAIL</td>");
105          }
106        setTimeout(benchmarkList(list, times, libraries), 100);
107      } else if(libraries[1]) {
108        benchmark(jQuery.benchmarker.startingList, times, libraries.slice(1));
109      } else {
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");
114        });
115        setTimeout(count, 100);
116      }
117    }
118
119   function benchmarkList(list, times, libraries) {
120     return function() {
121       benchmark(list.slice(1), times, libraries);
122     }
123   }
124
125  function count() {
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>");
131    }
132  }
133
134
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;
140    }));
141    return this.filter(function() {
142      if( withinTolerance(parseFloat(this.innerHTML.replace(/[^\.\d]/g, "")), target, tolerance, percentage) ) return true;
143    })
144  }
145
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) }
148
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;
153  }
154
155  Math.sum = function(arr) {
156    var sum = 0;
157    for(i = 0; i < arr.length; i++) sum += arr[i];
158    return sum;
159  }