typeahead: Precompile regexes for removing diacritics.

Precompiling regexes gives a performance increase of around 10-15%
based on tests. See https://jsperf.com/typeahead-regex. This stacks
up when we have a lot of users in an organisation.
This commit is contained in:
Rohitt Vashishtha 2019-09-19 11:32:49 +05:30 committed by Tim Abbott
parent b72bb8171b
commit 6a08efc065
1 changed files with 19 additions and 8 deletions

View File

@ -759,18 +759,29 @@ exports.incr_recipient_count = function (user_id) {
// Diacritic removal from:
// https://stackoverflow.com/questions/18236208/perform-a-find-match-with-javascript-ignoring-special-language-characters-acce
const diacritic_regexes = {
letters_only: /^[a-z]+$/,
a: /[áàãâä]/g,
e: /[éèëê]/g,
i: /[íìïî]/g,
o: /[óòöôõ]/g,
u: /[úùüû]/g,
c: /[ç]/g,
n: /[ñ]/g,
};
exports.remove_diacritics = function (s) {
if (/^[a-z]+$/.test(s)) {
if (diacritic_regexes.letters_only.test(s)) {
return s;
}
return s.replace(/[áàãâä]/g, "a")
.replace(/[éèëê]/g, "e")
.replace(/[íìïî]/g, "i")
.replace(/[óòöôõ]/g, "o")
.replace(/[úùüû]/g, "u")
.replace(/[ç]/g, "c")
.replace(/[ñ]/g, "n");
return s.replace(diacritic_regexes.a, "a")
.replace(diacritic_regexes.e, "e")
.replace(diacritic_regexes.i, "i")
.replace(diacritic_regexes.o, "o")
.replace(diacritic_regexes.u, "u")
.replace(diacritic_regexes.c, "c")
.replace(diacritic_regexes.n, "n");
};
exports.person_matches_query = function (user, query) {