Vince
Vince

Reputation: 4439

javascript: fastest way finding is a string is in a set of string

I need a fast way to find if a string is in a set of strings.

My set does not change much over time, so putting it in a sorted array and using binary search is an option (as advised here: fastest way to determine if an element is in a sorted array)

But would using a trie be faster considering we are talking about String ? If so, is there a well known and supported implementation I could use ? (found a few one on github, but did not seem supported or broadly used).

I was also reading: Fast way to find if a string is in an array

Any chance this approach could beat using a trie ?

(I do not have the time to try to implement all the approaches and benchmark them.)

Upvotes: 0

Views: 263

Answers (1)

Amadan
Amadan

Reputation: 198436

You have JavaScript. If you go with trie, it would be your own implementation in JavaScript, while a hash is pretty much the foundation the whole JavaScript is built on, optimised to hell in the execution environment. I'd just do this:

var STRINGS = {
  "foo": true,
  "bar": true
}

var fooExists = STRINGS.hasOwnProperty("foo");

Upvotes: 2

Related Questions