2016-04-24 13:41:20 -04:00
|
|
|
import {SelectorMatcher, CssSelector} from "angular2/src/compiler/selector";
|
2015-11-06 17:34:07 -08:00
|
|
|
import {StringWrapper, Math} from 'angular2/src/facade/lang';
|
2015-10-13 00:29:13 -07:00
|
|
|
import {getIntParameter, bindAction} from 'angular2/src/testing/benchmark_util';
|
2015-11-17 15:24:36 -08:00
|
|
|
import {BrowserDomAdapter} from 'angular2/src/platform/browser/browser_adapter';
|
2014-10-28 14:46:55 -07:00
|
|
|
|
2014-11-17 11:01:21 -08:00
|
|
|
export function main() {
|
2015-02-27 14:50:06 -08:00
|
|
|
BrowserDomAdapter.makeCurrent();
|
2015-01-09 18:00:04 -08:00
|
|
|
var count = getIntParameter('selectors');
|
|
|
|
|
2014-12-22 17:50:10 -08:00
|
|
|
var fixedMatcher;
|
|
|
|
var fixedSelectorStrings = [];
|
|
|
|
var fixedSelectors = [];
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < count; i++) {
|
2015-06-17 11:17:21 -07:00
|
|
|
fixedSelectorStrings.push(randomSelector());
|
2014-10-28 14:46:55 -07:00
|
|
|
}
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < count; i++) {
|
2015-06-17 11:17:21 -07:00
|
|
|
fixedSelectors.push(CssSelector.parse(fixedSelectorStrings[i]));
|
2014-10-28 14:46:55 -07:00
|
|
|
}
|
|
|
|
fixedMatcher = new SelectorMatcher();
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < count; i++) {
|
2015-03-19 17:01:42 +01:00
|
|
|
fixedMatcher.addSelectables(fixedSelectors[i], i);
|
2014-10-28 14:46:55 -07:00
|
|
|
}
|
2014-12-22 17:50:10 -08:00
|
|
|
|
2015-01-09 18:00:04 -08:00
|
|
|
function parse() {
|
2014-12-22 17:50:10 -08:00
|
|
|
var result = [];
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < count; i++) {
|
2015-06-17 11:17:21 -07:00
|
|
|
result.push(CssSelector.parse(fixedSelectorStrings[i]));
|
2014-12-22 17:50:10 -08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:00:04 -08:00
|
|
|
function addSelectable() {
|
2014-12-22 17:50:10 -08:00
|
|
|
var matcher = new SelectorMatcher();
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < count; i++) {
|
2015-03-19 17:01:42 +01:00
|
|
|
matcher.addSelectables(fixedSelectors[i], i);
|
2014-12-22 17:50:10 -08:00
|
|
|
}
|
|
|
|
return matcher;
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:00:04 -08:00
|
|
|
function match() {
|
2014-12-22 17:50:10 -08:00
|
|
|
var matchCount = 0;
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
fixedMatcher.match(fixedSelectors[i][0], (selector, selected) => { matchCount += selected; });
|
2014-12-22 17:50:10 -08:00
|
|
|
}
|
|
|
|
return matchCount;
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:00:04 -08:00
|
|
|
bindAction('#parse', parse);
|
|
|
|
bindAction('#addSelectable', addSelectable);
|
|
|
|
bindAction('#match', match);
|
2014-10-28 14:46:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function randomSelector() {
|
|
|
|
var res = randomStr(5);
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < 3; i++) {
|
|
|
|
res += '.' + randomStr(5);
|
2014-10-28 14:46:55 -07:00
|
|
|
}
|
2015-05-29 21:40:15 -07:00
|
|
|
for (var i = 0; i < 3; i++) {
|
|
|
|
res += '[' + randomStr(3) + '=' + randomStr(6) + ']';
|
2014-10-28 14:46:55 -07:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:40:15 -07:00
|
|
|
function randomStr(len) {
|
2014-10-28 14:46:55 -07:00
|
|
|
var s = '';
|
|
|
|
while (s.length < len) {
|
|
|
|
s += randomChar();
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:40:15 -07:00
|
|
|
function randomChar() {
|
2014-10-28 14:46:55 -07:00
|
|
|
var n = randomNum(62);
|
2015-05-29 21:40:15 -07:00
|
|
|
if (n < 10) return n.toString(); // 1-10
|
|
|
|
if (n < 36) return StringWrapper.fromCharCode(n + 55); // A-Z
|
|
|
|
return StringWrapper.fromCharCode(n + 61); // a-z
|
2014-10-28 14:46:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function randomNum(max) {
|
|
|
|
return Math.floor(Math.random() * max);
|
|
|
|
}
|