2017-06-27 18 views
0

我有一個JavaScript,它執行大量計算並將結果以許多數組的形式繪製到Google Charts對象中。在不重新加載頁面的情況下,我可以應用新值並一次又一次地運行分析,每次繪製結果。奇怪的是,這在Chrome中運行良好,但在MacOS上的Safari中,每次新的分析都會變得更慢,並且需要重新加載頁面才能恢復速度。但是,在Chrome桌面和移動設備以及Safari移動設備中,它始終以最高速度運行,無需重新加載。我懷疑Safari macOS正在緩存一些東西並填充內存。我使用了一堆每次創建的圖形對象(代碼如下)(但大概也是垃圾回收)。爲什麼我的JavaScript在Safari桌面放慢速度,但不是Chrome或Safari移動?

 var chart01 = new google.visualization.LineChart(document.getElementById('chart_01')); 
    var chart02 = new google.visualization.LineChart(document.getElementById('chart_02')); 
    var chart03 = new google.visualization.LineChart(document.getElementById('chart_03')); 
    var chart04 = new google.visualization.LineChart(document.getElementById('chart_04')); 
    var chart05 = new google.visualization.LineChart(document.getElementById('chart_05')); 
    var chart06 = new google.visualization.LineChart(document.getElementById('chart_06')); 
    var chart07 = new google.visualization.LineChart(document.getElementById('chart_07')); 
    var chart08 = new google.visualization.LineChart(document.getElementById('chart_08')); 
    var chart09 = new google.visualization.LineChart(document.getElementById('chart_09')); 
    var chart10 = new google.visualization.LineChart(document.getElementById('chart_10')); 
    var chart11 = new google.visualization.LineChart(document.getElementById('chart_11')); 
    var chart12 = new google.visualization.LineChart(document.getElementById('chart_12')); 
    var chart13 = new google.visualization.LineChart(document.getElementById('chart_13')); 
    var chart14 = new google.visualization.LineChart(document.getElementById('chart_14')); 
    var chart15 = new google.visualization.LineChart(document.getElementById('chart_15')); 
+0

嘗試使用內存分析器來查看是否發生了這種情況。 – Barmar

回答

0

理想情況下,您應該用新數據更新圖表,而不是每次都創建它們。這是爲了避免任何類型的垃圾收集延遲。

+0

感謝您的建議。我試圖將聲明放在添加數據的函數之外,但是我收到一個錯誤:「Uncaught TypeError:無法讀取未定義的屬性'LineChart'」,並且圖表不起作用。可能是我無法解決的範圍問題。我不知道如何讓圖表對象可用於分配數據而不使它們成爲全局數據的函數,我認爲唯一的選擇是將它們作爲參數傳遞,這會非常麻煩,因爲數量太多了。 –

相關問題