An alternative to regular expression is that you can store the letters of the name individually, with the 'matches' element at each level containing names matching that value (should be pretty fast, but if you have a large number of names, the array will be huge).
array | - m | - matches | - - 'Abraham Maslow' | - - 'John Motson' | - a | - - matches | - - - 'Abraham Maslow' | - - s | - - - matches | - - - 'Abraham Maslow' | - - - l | - - - - matches | - - - - - 'Abraham Maslow' ... | - s | - - matches | - - 'Johnathan Smith' | - - m | - - - matches | - - - - 'Johnathan Smith' | - - - - i
This should be well optimized for speed, because you can just do something like this to find the name:
var initials = initial.split(''); var matches; for (var x in initials) { matches = initials[x]; } matches = matches['matches']; // now contains ['Abraham Maslow','John Motson'] or ['Abraham Maslow'], etc
That way, you never go down a branch that has something else besides what interests you, so you will never see “Jonathan Smith” when the name does not start with “S” and never counts “John Motson "when the name begins with" Ma "instead of" Mo ", etc.
source share