Chrome立刻做了什么,Firefox需要30秒

Ard*_*eol 17 javascript performance

目前,我正在创建一个程序,将源代码转换为突出显示的HTML文本.但是,当我测试它时,我发现了一些奇怪的结果.在Chrome上,该程序几乎可以即时解析1000行源代码.但是,Firefox需要30秒才能解析相同的1000行.而且,具有讽刺意味的是,IE10只需要18秒.

现在,我了解不同的浏览器以不同的方式实现javascript,并且Chrome往往更快,但我不明白为什么它会使Firefox超过30倍.我在每个操作上进行了10,000,000,000次操作的原始循环测试,并且花了14秒和Chrome 12.因此,我倾向于相信我的代码中某处需要Firefox需要很长时间才能完成; 我做过研究,但到目前为止我发现的任何内容都没有表明我所看到的巨大差异.

那么,有没有人对可能导致这种情况的原因有任何建议?我已经发布了下面代码的问题区域(注释掉这部分导致两个浏览器即时解析). start并且end都是正则表达式; istream是源代码的来源,ostream也是解析代码所在的位置. istream.read()调用String slice()方法.最后,在整个程序中多次调用此函数.

function(buffer, istream, ostream){
    if(start.test(istream.content)){
        buffer = istream.read();
        ostream.write('[[span class="' + type + '"]]' + buffer);
        do{
            /* Special Cases */
            if(end.test(ostream.content + istream.peek()) && (istream.peek() == "\n" || istream.peek() == " " || istream.peek() == "\t")){
                include = true;
                break;
            }
            else if(istream.peek() == "\n"){
                istream.read();
                ostream.write('[[/span]][[/span]]\n[[span class="line"]][[span class="' + type + '"]]');
                continue;
            }
            else if(istream.peek() == "\t"){
                istream.read();
                ostream.write("@<&#160;&#160;&#160;&#160;>@");
                continue;
            }
            else if(istream.peek() == " "){
                istream.read();
                ostream.write("@<&#160;>@");
                continue;
            }
            ostream.write(istream.read());
        } while(!istream.isEmpty() && !end.test(ostream.content));

        if(include || istream.isEmpty())
            ostream.write('[[/span]]');
        else{
            var ending = ostream.content.length-1;
            while(!end.test(ostream.content.substr(ending)))
                --ending;
            istream.content = ostream.content.substr(ending) + istream.content;
            ostream.content = ostream.content.substring(0, ending) + '[[/span]]';
        }
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我们将非常感谢您的任何见解,如果您对如何实施某些方面有任何疑问,我将不胜感激.提前致谢.

istream和ostream对象的定义:

function IOstream(init){
    this.content = init;

    this.read = function(){
        var tmp = this.content.charAt(0);
        this.content = this.content.slice(1);
        return tmp;
    };
    this.peek = function(){  return this.content.charAt(0); };
    this.write = function(str){  this.content += str; };
    this.isEmpty = function(){  return this.content.length == 0; }
}
Run Code Online (Sandbox Code Playgroud)

dmi*_*kam 1

我认为这是因为在.read()您进行的每次调用content.slice(1)以及每次复制整个字符串(但第一个字符)时,可能会花费很多时间。尝试像这样修改 IOStream 类:

function IOstream(init){
    this.content = init;
    this.cursor = 0;

    this.read = function(){
        var tmp = this.content.charAt(this.cursor);
        this.cursor++;
        return tmp;
    };
    this.peek = function(){  return this.content.charAt(this.cursor); };
    this.write = function(str){  this.content += str; };
    this.isEmpty = function(){  return this.cursor>=this.content.length; }
}
Run Code Online (Sandbox Code Playgroud)

我认为它将解决您在所有浏览器中的速度问题。