2015-04-02 14:40:49 -07:00
|
|
|
import {SelectorMatcher} from "angular2/src/render/dom/compiler/selector";
|
|
|
|
import {CssSelector} from "angular2/src/render/dom/compiler/selector";
|
2015-02-05 13:08:05 -08:00
|
|
|
import {StringWrapper, Math} from 'angular2/src/facade/lang';
|
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {getIntParameter, bindAction} from 'angular2/src/test_lib/benchmark_util';
|
2015-02-27 14:50:06 -08:00
|
|
|
import {BrowserDomAdapter} from 'angular2/src/dom/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-01-09 18:00:04 -08:00
|
|
|
for (var i=0; i<count; i++) {
|
2014-10-28 14:46:55 -07:00
|
|
|
ListWrapper.push(fixedSelectorStrings, randomSelector());
|
|
|
|
}
|
2015-01-09 18:00:04 -08:00
|
|
|
for (var i=0; i<count; i++) {
|
2014-10-28 14:46:55 -07:00
|
|
|
ListWrapper.push(fixedSelectors, CssSelector.parse(fixedSelectorStrings[i]));
|
|
|
|
}
|
|
|
|
fixedMatcher = new SelectorMatcher();
|
2015-01-09 18:00:04 -08: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-01-09 18:00:04 -08:00
|
|
|
for (var i=0; i<count; i++) {
|
2014-12-22 17:50:10 -08:00
|
|
|
ListWrapper.push(result, CssSelector.parse(fixedSelectorStrings[i]));
|
|
|
|
}
|
|
|
|
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-01-09 18:00:04 -08: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-01-09 18:00:04 -08:00
|
|
|
for (var i=0; i<count; i++) {
|
2015-03-19 17:01:42 +01:00
|
|
|
fixedMatcher.match(fixedSelectors[i][0], (selector, selected) => {
|
2014-12-22 17:50:10 -08:00
|
|
|
matchCount += selected;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
for (var i=0; i<3; i++) {
|
|
|
|
res += '.'+randomStr(5);
|
|
|
|
}
|
|
|
|
for (var i=0; i<3; i++) {
|
|
|
|
res += '['+randomStr(3)+'='+randomStr(6)+']';
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomStr(len){
|
|
|
|
var s = '';
|
|
|
|
while (s.length < len) {
|
|
|
|
s += randomChar();
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomChar(){
|
|
|
|
var n = randomNum(62);
|
2014-11-14 14:03:03 -08:00
|
|
|
if(n<10) return n.toString(); //1-10
|
2014-10-28 14:46:55 -07:00
|
|
|
if(n<36) return StringWrapper.fromCharCode(n+55); //A-Z
|
|
|
|
return StringWrapper.fromCharCode(n+61); //a-z
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomNum(max) {
|
|
|
|
return Math.floor(Math.random() * max);
|
|
|
|
}
|