Whats the fastest way to highlight different strings in an html document?

Giles Butler

I'm trying to search for strings in an html page, I have an object with 1000 names in it and I need to find if those names exist in the html document. If they do exist i then need to wrap them in a span or something and apply a class.

I've been using this jQuery Highlight Plugin but it takes about 17 seconds to scan and highlight the whole page, this obviously varies depending on how many different names there are on the page.

I've tried a few different plugins but haven't found anything faster, is there a way to do this in vanilla JS that is faster? It only needs to work in Chrome.

Any help would be really appreciated.

Thanks

Edit: Structure of the object which holds names:

var PEOPLE = { "fred" : { loaded : false, elements : [] }, "dave" : { loaded : false, elements : [] }, "bob" : { loaded : false, elements : [] } }

Calummm

Plain javascript.

Basically I iterate through your object and extract all the relevant names. Once I have this, I pass the array to the highlightString function where I join all the strings you want to match with the pipe character and create a regular expression that runs globally and ignores case. This results in the div being searched only being searched once. This should result in a considerable speedup.

var PEOPLE = {
  "fred" : {
    loaded : false,
    elements : []
  },
  "dave" : {
    loaded : false,
    elements : []
  },
  "bob" : {
    loaded : false,
    elements : []
  }
},
name,
search = [];

for (name in PEOPLE) {
  search.push(name);
}    

function highlightStrings(input, toMatch) {
  var reg = new RegExp('(' + toMatch.join('|') + ')', "gi");
  input.innerHTML = input.innerHTML.replace(reg, '<span class="myHighLight">$1</span>');  
}

highlightStrings(document.getElementById("test"), search);
.myHighLight {background-color:yellow;}
<div id="test">
  test1 test2 fred test4 test5 test6 test7 test8 dave test10 test11 test12 bob test14 test15
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

whats the fastest way to find eigenvalues/vectors in python?

whats the fastest way to write join in linq

Fastest/Most efficient way to parse a document, search for strings and replace them in document with Java

whats the fastest way to find the densest region in an array or list?

Whats fastest way for finding closest number pair in array of pairs

Whats the fastest way to ignore case when using re in python?

Whats the fastest way to loop through sorted dask dataframe?

whats is the best/fastest way to upload images from android to mysql?

Is there a "fastest way" to construct Strings in Java?

Fastest way to extract matching strings

Fastest Way for Comparing Strings Python

Fastest way of comparing strings in C

i need to find a way to split an xml document into different strings

Whats the fastest way to remove all occurrences of a value from an array is splicing it the fastest or is filter faster?

The fastest way to find highest number in document

Whats is the best way to check URL document link existence?

Whats the best way to implement a simple document management system?

Fastest way to expose C strings to NumPy?

Which is the fastest way of querying strings in python

Fastest way to convert strings to dates in R

C: What is the best and fastest way to concatenate strings

Fastest way to replace multiple strings in a huge string

What is the fastest way to save set of strings in SharedPreferences?

Fastest way to identify rows containing strings

Fastest way to perform a lot of strings replace in Java

Fastest way to compare strings (literal and numerical)

Java fastest way to concatenate strings, integers and floats

Fastest way to find string in the array of strings

fastest way to filter array of strings in JavaScript