Subversion Repositories JSX

Compare Revisions

Last modification

Ignore whitespace Rev 589 → Rev 590

/trunk/string/parser/test/profiling.js
0,0 → 1,74
 
function profile (algorithm, profileName)
{
var s = "bazbarfoo";
 
console.profile(profileName);
 
for (var i = 1000; i--;)
{
algorithm(s);
}
 
console.profileEnd(profileName);
}
 
var tokens = [
{pattern: /foo/},
{pattern: /bar/, state: "bar"},
{pattern: /baz/, state: "S"}
];
var rx = new RegExp("(" + tokens.map(function (e) { return e.pattern.source; }).join(")|(") + ")", "g");
var global_tokens = tokens.map(function (e) {
return {pattern: new RegExp(e.pattern.source, "g"), state: e.state};
});
var l = tokens.length;
 
function compiled (s)
{
var state = null;
var m;
while ((m = rx.exec(s)))
{
for (var i = 0; i < l; ++i)
{
if (m[i + 1])
{
}
}
}
}
 
function iterative (s)
{
var last_index = 0;
 
do
{
var index = Infinity;
var used_m = null;
var used_rx = null;
 
for (var i = 0; i < l; ++i)
{
var rx = global_tokens[i].pattern;
rx.lastIndex = last_index;
var m = rx.exec(s);
 
if (m && m.index < index)
{
used_rx = rx;
used_m = m;
index = used_m.index;
}
}
 
if (used_m)
{
last_index = used_rx.lastIndex;
}
} while (used_m);
}
 
profile(compiled, "compiled");
profile(iterative, "iterative");
/trunk/string/parser/test/profiling.html
0,0 → 1,13
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Parser Profiling: First match wins – Compiled expression vs. one-by-one matching</title>
<script type="text/javascript" src="profiling.js"></script>
</head>
<body>
<h1>Parser Profiling: First match wins – Compiled expression vs. one-by-one matching</h1>
</body>
</html>