我有一個Chrome擴展,需要瀏覽網頁上的每個<p></p>
。我的Chrome擴展插件查看p
文本並檢查文本是否位於數組中。問題是,該陣列有超過3000個元素,如果可能的話,我非常想要達到12,000或更多。
以目前的速度,這是不可行的,因爲它需要網頁約4秒多加載頁面。我的Chrome擴展在文檔的末尾運行,因此用戶可以在技術上瀏覽網站,只需要4秒鐘就可以加載所有內容。
這裏是我的代碼:
$.each(arrayObj, function(key, value) {
$("p").highlight(key, {caseSensitive: true, className: 'highlight-882312', wordsOnly:true });
});
$('.highlight-882312').each(function() {
var currentKey = $(this).text();
console.log(currentKey);
//do something else here that doesn't really
//apply to the question as it only runs when the user hovers over the class.
});
,然後陣列看起來像這樣很簡單:
var arrayObj = {
"keyword1": 'keyword_1_site',
"keyword2": 'keyword_2_site',
... etc
... 3,000+ more lines ...
...
}
我假設$.each
是不是最有效的,正如我所說,加載4秒是相當多的。有什麼我可以做的,以提高效率?我能否在陣列中擊中12,000行?
謝謝:)
,如果你需要,你可以使用一些研究算法,就像二分搜索的搜索方法。 – Drago96