2013-11-20 21 views
17

現在、私は、ソースコードをハイライトされたHTMLのようなテキストに変換するプログラムを作成しています。私はそれをテストしたが、私はいくつかの奇妙な結果を発見した。 Chromeでは、プログラムはほぼ即座に1000行のソースを解析します。しかし、Firefoxは同じ1000行を解析するのに30秒かかります。皮肉なことに、IE10は18秒しかかかりません。Chromeは即座に実行し、Firefoxは30秒かかる

さまざまなブラウザでjavascriptが異なって実装されていること、Chromeが高速になる傾向があることを理解していますが、なぜ30日間以上Firefoxを使用しているのかわかりません。私は10億回の生のwhileループテストをそれぞれ実行していましたが、FF 14秒とChrome 12がかかっていました。私のコードのどこかで、Firefoxの実行に異常に長い時間がかかると思われがちです。私は研究をしましたが、私が今までに見つけたことは、私が見ている大きな矛盾を示すものではありません。

だから、これを引き起こしている可能性のあることは誰にもありますか?私は以下のコードの問題領域を掲示しました(この部分をコメントアウトすると、両方のブラウザーが瞬時に解析されます)。 startendは両方とも正規表現です。 istreamはソースコードの出所であり、ostreamは解析されたコードの出所です。 istream.read()は、文字列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; 
} 

任意の洞察力をいただければ幸いです、そしてあなたは、この特定の態様が実施されている方法として、任意のクエリを持っている場合、私は義務付けるます。前もって感謝します。 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; } 
} 
+1

あなたはRESが( 'start'と' end')どのように見えるか私たちを見ることができますか? – Flimzy

+0

ostream.writeはどこに行きますか?文字列にするか、またはDOMなどに直接? – user2864740

+1

おそらく原因ではないかもしれませんが、すぐに変わるべきことは、複数の 'istream.peek()'呼び出しを 'do'文の前に単一の呼び出しに移動することです。 – Graham

答えて

1

は、私はすべての.read()が呼び出しにあなたがcontent.slice(1)を作るので、それはあると思うたびに、それ全体をコピーした文字列が、最初の文字とは、多くの時間を取ることができます。 このようなあなたのIOStreamクラスをmodifyin試してみてください。

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

私はそれがすべてのブラウザで、あなたの速度の問題を解決すると思います。

関連する問題