简体   繁体   English

如何提高此JS函数的速度

[英]How can I improve the speed of this JS function

I have the following function which replaces links in a contenteditable textarea. 我具有以下功能,该功能可替换可编辑内容的文本区域中的链接。 It works, but gets slow around 100 characters. 它可以工作,但是变慢了大约100个字符。 How can I optimize this function to be faster? 如何优化此功能使其更快?

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;
}

You do not have to check for each word repeatedly. 您不必反复检查每个单词。 All you need to do is put the words in the string in a hash and then create the hyperlinked word once for each of the cases. 您需要做的就是将单词放在字符串中的哈希值中,然后为每种情况创建一次超链接单词。 Then replace ONLY the words that have been changed. 然后仅替换已更改的词。 Here is how I would do it. 这就是我要怎么做。 Hope this helps. 希望这可以帮助。

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;
}

In case you do not mind loosing the extra spaces you might want to replace the lower part of code to the following 如果您不介意浪费多余的空间,则可以将下面的代码替换为以下内容

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(" ");

You can replace this lines 您可以替换此行

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

with a regular expression verification like: 使用正则表达式验证,例如:

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

You will have much more speed with the same result! 同样的结果,您将获得更快的速度!

You need to split algorithm into two separate loops (not one in another), using a hash of words. 您需要使用单词的哈希将算法分为两个单独的循环(而不是一个)。 It would look like (not tested, but you have an idea): 它看起来像(未经测试,但您有个主意):

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 ...
    ....
    ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM