Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Use of RegEx is high performance hit and unnecessary. #129

Closed
apboon opened this issue Jan 22, 2019 · 2 comments
Closed

[BUG] Use of RegEx is high performance hit and unnecessary. #129

apboon opened this issue Jan 22, 2019 · 2 comments

Comments

@apboon
Copy link

apboon commented Jan 22, 2019

Use of RegEx circumvents MongoDB's ability to rely on indexes. Maybe its better to add lowercase: true to the schema of the email address field and simply check against the lower-case value?

return this.model(COLLECTION).findOne({email: new RegExp('^' + email + '$', 'i')}, callback);


Here the use of RegEx is a major performance hit, making the application feel sluggish. Besides this, it also limits usability; if a ticket contains companyname 123caseid (accidental double space when the ticket was created), a search for companyname 123caseid (single space) or 123caseid companyname won't yield any results.

https://github.com/polonel/trudesk/blob/master/src/models/ticket.js#L916

I recommend using MongoDB's full text search. Here's an example for creating an index:

schema.index(
 {
  'name.personal': 'text',
  'name.family': 'text',
  'name.display': 'text',
 },
 {
  name: 'name',
  weights:
   {
    'name.personal': 10,
    'name.family': 5,
    'name.display': 15,
   }
 });

In the example above I have given more weight to the display name, so that anything that matches the display name is prioritized.

Full text search also yields results when mistyping "companie" instead of "company". Not sure on the latest of MongoDB, but also might yield results when dealing with diacritics.

@apboon apboon changed the title Use of RegEx is high performance hit (doesn' and unnecessary Use of RegEx is high performance hit and unnecessary. Jan 22, 2019
@polonel
Copy link
Owner

polonel commented Jan 22, 2019

Perfect timing. I'm in the process right now of refactoring a lot of old outdated code from years back. You've spotted two great examples of that old code. I will get these refactored.

@polonel polonel changed the title Use of RegEx is high performance hit and unnecessary. [BUG] Use of RegEx is high performance hit and unnecessary. Jan 28, 2019
polonel added a commit that referenced this issue Jan 30, 2019
@polonel polonel closed this as completed Mar 9, 2019
@apboon
Copy link
Author

apboon commented Mar 9, 2019

@polonel Congrats! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants