javascript - My $.each loop is quite slow. Any methods of making it faster? -


i have chrome extension needs through every <p></p> on webpage. chrome extension looks @ p text , checks if text located in array. problem is, array has on 3,000 elements , i'd quite 12,000 or more if possible.

at current rate, isn't feasible because takes webpage 4 seconds load page. chrome extension runs @ end of document user can technically browse site, takes 4 seconds load.

here code:

$.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 else here doesn't  //apply question runs when user hovers on class.  }); 

and array looks pretty simple this:

var arrayobj = {  "keyword1": 'keyword_1_site', "keyword2": 'keyword_2_site', ... etc ... 3,000+ more lines ... ...  } 

i'm assuming $.each isn't efficient, , said, 4 seconds load quite bit. there make more efficient? ever able hit 12,000 lines in array?

thanks :)

you running global selectors each element in array. that's quite lot.

so @ least suggest replace this:

$.each(arrayobj, function(key, value) {       $("p").highlight(key, ...); }); 

by this:

var $all_p = $("p");  $.each(arrayobj, function(key, value) {     $all_p.highlight(key, ...); }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -