-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,150 @@ | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<meta charset="utf-8"> | |||
<title>XRegExp Perf</title> | |||
|
|||
<!-- XRegExp --> | |||
<script src="../src/xregexp.js"></script> | |||
<script src="../src/addons/build.js"></script> | |||
<script src="../src/addons/matchrecursive.js"></script> | |||
<script src="../src/addons/prototypes.js"></script> | |||
<script src="../src/addons/unicode/unicode-base.js"></script> | |||
<script src="../src/addons/unicode/unicode-blocks.js"></script> | |||
<script src="../src/addons/unicode/unicode-categories.js"></script> | |||
<script src="../src/addons/unicode/unicode-properties.js"></script> | |||
<script src="../src/addons/unicode/unicode-scripts.js"></script> | |||
|
|||
<!-- Very basic benchmarking, adapted from <https://github.com/visionmedia/js-bm> --> | |||
<script> | |||
(function(){ | |||
var currentSuite; | |||
|
|||
function pad(str, len) { | |||
return str + (new Array(len - str.length)).join(' '); | |||
} | |||
|
|||
function time(fn) { | |||
var start = Number(new Date); | |||
fn(); | |||
return (Number(new Date) - start) / 1000; | |||
} | |||
|
|||
benchmark = function(label, fn) { | |||
var duration = time(function() { | |||
for (var i = 0; i < currentSuite.times; ++i) { | |||
fn(); | |||
} | |||
}).toFixed(3); | |||
print(pad(' ' + label, 50 - String(duration).length) + duration + ' |'); | |||
}; | |||
|
|||
suite = function(label, times, fn) { | |||
currentSuite = this; | |||
this.times = times; | |||
print('\n ' + pad(label, 42 - String(this.times).length) + this.times + ' time(s)'); | |||
print(' -------------------------------------------------'); | |||
fn(); | |||
}; | |||
}()); | |||
</script> | |||
</head> | |||
<body> | |||
<pre id="log">Running tests...</pre> | |||
|
|||
<script> | |||
(function() { | |||
var msgs = ''; | |||
var log = document.getElementById('log'); | |||
|
|||
print = function(msg) { | |||
msgs += msg + '\n'; | |||
}; | |||
|
|||
print.commit = function() { | |||
log.innerHTML = msgs; | |||
}; | |||
}()); | |||
|
|||
suite('Constructor', 1e4, function() { | |||
var pattern = '^([.])\\1+$'; | |||
|
|||
benchmark('XRegExp() + pattern cache flush', function() { | |||
XRegExp(pattern, 'g'); | |||
XRegExp.cache.flush('patterns'); | |||
}); | |||
|
|||
benchmark('XRegExp()', function() { | |||
XRegExp(pattern, 'g'); | |||
}); | |||
|
|||
benchmark('XRegExp.cache()', function() { | |||
XRegExp.cache(pattern, 'g'); | |||
}); | |||
|
|||
benchmark('RegExp()', function() { | |||
new RegExp(pattern, 'g'); | |||
}); | |||
}); | |||
|
|||
suite('exec', 1e4, function() { | |||
var regexG = /(\b(?=x).(?=x).()??\2)+/g; | |||
var str = Array(50 + 1).join('hello x world') + ' xx!'; | |||
var strs = []; | |||
var pos = 5; | |||
|
|||
XRegExp.install('natives'); | |||
var fixedExec = RegExp.prototype.exec; | |||
XRegExp.uninstall('natives'); | |||
|
|||
// Generate all-different strings so Opera can't cheat with its regex/string match cache | |||
for (var i = 0; i < 1e4; ++i) { | |||
strs.push(str + i); | |||
} | |||
|
|||
i = 0; | |||
benchmark('Native RegExp::exec()', function() { | |||
regexG.lastIndex = pos; | |||
var match = regexG.exec(strs[i++]); | |||
}); | |||
|
|||
i = 0; | |||
benchmark('Shimmed RegExp::exec()', function() { | |||
regexG.lastIndex = pos; | |||
var match = fixedExec.call(regexG, strs[i++]); | |||
}); | |||
|
|||
i = 0; | |||
benchmark('XRegExp.exec()', function() { | |||
var match = XRegExp.exec(strs[i++], regexG, pos); | |||
}); | |||
|
|||
i = 0; | |||
benchmark('Native RegExp::exec() + sticky', function() { | |||
regexG.lastIndex = pos; | |||
var match = regexG.exec(strs[i++]); | |||
if (match && match.index !== pos) { | |||
match = null; | |||
} | |||
}); | |||
|
|||
i = 0; | |||
benchmark('Shimmed RegExp::exec() + sticky', function() { | |||
regexG.lastIndex = pos; | |||
var match = fixedExec.call(regexG, strs[i++]); | |||
if (match && match.index !== pos) { | |||
match = null; | |||
} | |||
}); | |||
|
|||
i = 0; | |||
// This should be the fastest in any browser that natively supports flag y | |||
benchmark('XRegExp.exec() + sticky', function() { | |||
var match = XRegExp.exec(strs[i++], regexG, pos, 'sticky'); | |||
}); | |||
}); | |||
|
|||
print.commit(); | |||
</script> | |||
</body> | |||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters