subzey, емнип, введение в регексп ИЛИ вызывает его сильное замедление (причем даже в перле). Ну, не говоря уже о replace
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>match VS replace</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="Geany 0.18.1.1" />
<script type="text/javascript">
function get_value() {
return document.getElementById('input').value;
}
function print_result(res) {
document.getElementById('output').appendChild(document.createElement('DIV')).
appendChild(document.createTextNode(res));
}
function test_match() {
var value = get_value(),
start = new Date().getTime();
// test
for (var i = 1000; i--; ) {
var words = value.match(/(\S+)/g);
var wordCount = words && words.length;
}
// end
var end = (new Date().getTime() - start);
print_result('Replace: ' + end + 'ms');
}
function test_replace() {
var value = get_value(),
start = new Date().getTime();
// test
for (var i = 1000; i--; ) {
var wordCount = 0;
value.replace(/(^|\s)+[^\s]+/g, function(a){wordCount++});
}
// end
var end = (new Date().getTime() - start);
print_result('Replace: ' + end + 'ms');
}
</script>
</head>
<body>
<p><textarea id="input" cols="40" rows="5">Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumyeirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diamvoluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumyeirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diamvoluptua. At vero eos et accusam
et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est
Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumyeirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diamvoluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumyeirmod tempor invidunt ut labore et dolore magna </textarea></p>
<p><button onclick="test_match()">Match</button><button onclick="test_replace()">Replace</button></p>
<div id="output"></div>
</body>
</html>