0

string to be checked for presence is a domain name (google.com) in a list of 10 million unique domain names of const type inside browser. Either I can concatenate 10million into 1 string so it is a problem of checking substring or I can use array index of to check the index.

This is similar to https://stackoverflow.com/a/1473742/2752107 and other related questions but does not address the scale.

  • 2
    What exactly do you want to check? If it's in the list? If some substring of it is in the list? Anything else? If it's just presence, then building a HashSet is the best option if you have to repeatedly check for presence of a String. – Joachim Sauer Oct 06 '20 at 18:43
  • Is "checking a string" finding if the string is in the array? – D. Pardal Oct 06 '20 at 18:46

2 Answers2

1

It depends on multiple factors.

Is the array sorted in any way? If so, perhaps something like a binary search is your best bet.

Do you want to find a specific string in an unsorted array and quit the loop once you have found it? you're probably best of using Array.prototype.find() or creating a for loop and breaking as soon as you have found a string.

Do you need to check every string in the array? A simple for loop can do the trick.

Rezaa91
  • 500
  • 2
  • 10
0

Either I can concatenate 10million into 1 string so it is a problem of checking substring or I can use array index of to check the index.

Checking for a substring is slower than checking it using indices.

As Rezaa91's answer suggests, if you can order the array, you can then perform a binary search which would be way faster than a linear search.

If you do want/need to search linearly, however, use a Web Worker so your website/application remains usable and responsive while the search is in progress.

D. Pardal
  • 6,173
  • 1
  • 17
  • 37
  • I think @joachim Sauer's answer of using Set is better because it is O(1). I can create sorted array. I am not sure if javascript Set is backed by TreeSet or HashSet. – Flat Screen Oct 06 '20 at 18:59