如何提高这个JS函数的速度

时间:2014-03-26 23:28:11

标签: javascript function search replace

我有以下功能替换了一个可信的textarea中的链接。它有效,但在100个字符左右变慢。如何才能更快地优化此功能?

function makeLinksFrom (str) {
    var wordArray = str.replace(/(<([^>]+)>)/ig,"").split(' ');
    var domainsArray = ['.com', '.net', '.co', '.ca', '.io', '.me'];

    wordArray.forEach(function(word) {
        domainsArray.forEach(function(domain) {
            if(word.indexOf(domain) != -1 && word.substr(word.length - 6) == '&nbsp;') {
                if(word.indexOf('http://') == -1) {
                    str = str.replace(word, '<a target="_blank" contenteditable="false" href="http://'+clean(word)+'">link</a>&nbsp;');
                } else {
                    str = str.replace(word, '<a target="_blank" contenteditable="false" href="'+clean(word)+'">link</a>&nbsp;');
                }
            }
        });
    });

    return str;
}

3 个答案:

答案 0 :(得分:1)

您不必重复检查每个单词。您需要做的就是将字符串中的单词放在哈希中,然后为每个案例创建一次超链接字。然后只替换已更改的单词。我就是这样做的。希望这会有所帮助。

function makeLinksFrom (str) {
    var wordArray = str.replace(/(<([^>]+)>)/ig,"").split(' ');
    var domainsArray = ['.com', '.net', '.co', '.ca', '.io', '.me'];

    var positions =  {};
    wordArray.forEach(function(word){
        var value = positions[word];
        positions[word] = 1;

    });

    var keys = Object.keys(positions);
    var cleanWord = {};
    keys.forEach(function(key){
       domainsArray.forEach(function(domain){
          if(key.indexOf(domain) != -1 && key.substr(word.length - 6) == '&nbsp;') {
            if(key.indexOf('http://') == -1){
                cleanWord[key] = '<a target="_blank" contenteditable="false" href="http://'+clean(word)+'">link</a>&nbsp;';
            }else{
                cleanWord[key] = '<a target="_blank" contenteditable="false" href="'+clean(word)+'">link</a>&nbsp;';
            }
         }
       });
    });

    keys.forEach(function(key){
      if(key != cleanWord[key])
        str = str.replace(key, cleanWord[key]);
    });

    return str;
}

如果您不介意丢失多余的空格,您可能需要将代码的下半部分替换为以下内容

keys.forEach(function(key){
       domainsArray.forEach(function(domain){
          if(key.indexOf(domain) != -1 && key.substr(word.length - 6) == '&nbsp;') {
            if(key.indexOf('http://') == -1){
                cleanWord[key] = '<a target="_blank" contenteditable="false" href="http://'+clean(word)+'">link</a>&nbsp;';
            }else{
                cleanWord[key] = '<a target="_blank" contenteditable="false" href="'+clean(word)+'">link</a>&nbsp;';
            }
          }else{
             cleanWord[key] = word;
          }
       });
    });
    var newArr = [];
    wordArray.forEach(function(word){
      newArr.push(cleanWord[word]);
    });

    return newArr.join(" ");

答案 1 :(得分:0)

您可以替换此行

domainsArray.forEach(function(domain) {
    if(word.indexOf(domain) != -1 && word.substr(word.length - 6) == '&nbsp;')

使用正则表达式验证,如:

if (work.match(/.+\\.(com|net|co|ca|io|me)/) != null && word.substr(word.length - 6) == '&nbsp;')

你会有更快的速度和相同的结果!

答案 2 :(得分:0)

您需要使用单词哈希将算法拆分为两个单独的循环(不是另一个循环)。它看起来像(没有经过测试,但你有一个想法):

var hash = {};
var key;

var i;
for (i=0; i<wordArray.length; i++) {
    for (var j=0; j<domainsArray.length; j++);
        key = word[i] + "/" + domain[j];
        hash[key] = key;
    }
}

for (key in hash) {
    word = hash[key].split('/')[0];
    domain = hash[key].split('/')[1];
    if (word.indexOf(domain) != -1 ...
    ....
    ....
}