Assign result properties to variables to reduce number of lookups

This commit is contained in:
toasted-nutbread 2020-02-15 17:23:29 -05:00
parent 69b5007842
commit 19fb7dacb4

View File

@ -177,11 +177,17 @@ function dictTermsMergeBySequence(definitions, mainDictionary) {
function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices=null) { function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices=null) {
const definitionsByGloss = appendTo !== null ? appendTo : new Map(); const definitionsByGloss = appendTo !== null ? appendTo : new Map();
const resultExpressionsMap = result.expressions;
const resultExpressionSet = result.expression;
const resultReadingSet = result.reading;
const resultSource = result.source;
for (const [index, definition] of definitions.entries()) { for (const [index, definition] of definitions.entries()) {
const {expression, reading} = definition; const {expression, reading} = definition;
if (mergedIndices !== null) { if (mergedIndices !== null) {
const expressionMap = result.expressions.get(expression); const expressionMap = resultExpressionsMap.get(expression);
if ( if (
typeof expressionMap !== 'undefined' && typeof expressionMap !== 'undefined' &&
typeof expressionMap.get(reading) !== 'undefined' typeof expressionMap.get(reading) !== 'undefined'
@ -200,7 +206,7 @@ function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices
reading: new Set(), reading: new Set(),
definitionTags: [], definitionTags: [],
glossary: definition.glossary, glossary: definition.glossary,
source: result.source, source: resultSource,
reasons: [], reasons: [],
score: definition.score, score: definition.score,
id: definition.id, id: definition.id,
@ -212,8 +218,8 @@ function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices
glossDefinition.expression.add(expression); glossDefinition.expression.add(expression);
glossDefinition.reading.add(reading); glossDefinition.reading.add(reading);
result.expression.add(expression); resultExpressionSet.add(expression);
result.reading.add(reading); resultReadingSet.add(reading);
for (const tag of definition.definitionTags) { for (const tag of definition.definitionTags) {
if (!glossDefinition.definitionTags.find((existingTag) => existingTag.name === tag.name)) { if (!glossDefinition.definitionTags.find((existingTag) => existingTag.name === tag.name)) {
@ -235,10 +241,10 @@ function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices
... ...
]); ]);
*/ */
let readingMap = result.expressions.get(expression); let readingMap = resultExpressionsMap.get(expression);
if (typeof readingMap === 'undefined') { if (typeof readingMap === 'undefined') {
readingMap = new Map(); readingMap = new Map();
result.expressions.set(expression, readingMap); resultExpressionsMap.set(expression, readingMap);
} }
let termTagsMap = readingMap.get(reading); let termTagsMap = readingMap.get(reading);
@ -257,13 +263,13 @@ function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices
for (const definition of definitionsByGloss.values()) { for (const definition of definitionsByGloss.values()) {
definition.only = []; definition.only = [];
if (!utilSetEqual(definition.expression, result.expression)) { if (!utilSetEqual(definition.expression, resultExpressionSet)) {
for (const expression of utilSetIntersection(definition.expression, result.expression)) { for (const expression of utilSetIntersection(definition.expression, resultExpressionSet)) {
definition.only.push(expression); definition.only.push(expression);
} }
} }
if (!utilSetEqual(definition.reading, result.reading)) { if (!utilSetEqual(definition.reading, resultReadingSet)) {
for (const reading of utilSetIntersection(definition.reading, result.reading)) { for (const reading of utilSetIntersection(definition.reading, resultReadingSet)) {
definition.only.push(reading); definition.only.push(reading);
} }
} }