From d5edcaf6a5070eb7fd38bd62852f955f8687734c Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 7 May 2019 10:02:40 -0600 Subject: [PATCH] Improve word render speed --- src/js/search.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/js/search.js b/src/js/search.js index 95c8d14..f249027 100644 --- a/src/js/search.js +++ b/src/js/search.js @@ -28,20 +28,24 @@ export function getSearchFilters() { export function getMatchingSearchWords() { const searchTerm = getSearchTerm(); const filters = getSearchFilters(); - const matchingWords = window.currentDictionary.words.slice().filter(word => { - if (!filters.allPartsOfSpeechChecked) { - const partOfSpeech = word.partOfSpeech === '' ? 'Unclassified' : word.partOfSpeech; - console.log('partOfSpeech', partOfSpeech); - return filters.partsOfSpeech.hasOwnProperty(partOfSpeech) && filters.partsOfSpeech[partOfSpeech]; - } - return true; - }).filter(word => { - const isInName = filters.name && new RegExp(searchTerm, 'g').test(word.name); - const isInDefinition = filters.definition && new RegExp(searchTerm, 'g').test(word.simpleDefinition); - const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(word.longDefinition); - return searchTerm === '' || isInName || isInDefinition || isInDetails; - }); - return matchingWords; + if (searchTerm !== '' || !filters.allPartsOfSpeechChecked) { + const matchingWords = window.currentDictionary.words.slice().filter(word => { + if (!filters.allPartsOfSpeechChecked) { + const partOfSpeech = word.partOfSpeech === '' ? 'Unclassified' : word.partOfSpeech; + console.log('partOfSpeech', partOfSpeech); + return filters.partsOfSpeech.hasOwnProperty(partOfSpeech) && filters.partsOfSpeech[partOfSpeech]; + } + return true; + }).filter(word => { + const isInName = filters.name && new RegExp(searchTerm, 'g').test(word.name); + const isInDefinition = filters.definition && new RegExp(searchTerm, 'g').test(word.simpleDefinition); + const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(word.longDefinition); + return searchTerm === '' || isInName || isInDefinition || isInDetails; + }); + return matchingWords; + } + + return window.currentDictionary.words } export function highlightSearchTerm(word) {