I'm just looking for general guidance on this short TypeScript function I implemented. Are there any glaring violations? Are there better practices for accomplishing this?

Should I be using map/reduce/filter, or is this simple approach just fine here? I'm not sure about efficiency here because we basically just need to examine every item from each list. I don't think there is a shortcut way to make this faster, but that's why I'm asking. For what it's worth, we're only talking about examining a couple hundred items.

The function takes two lists. If the source list contains items that the other list does not, we need to return those items. When comparing the lists, we need to remove leading and trailing whitespace, and we need to ignore case sensitivity. However, the returned values need to be in their original casing.

 * Returns a list of items missing from a source list. The items are compared while ignoring case and
 * ignoring leading and trailing whitespace. The values returned maintain their original casing though.
 * @param sourceList - The master list used for comparison.
 * @param otherList  - The list to be compared to the source list.
 * @returns The items in the source list that are missing from the other list.
    export function getMissingItemsIgnoreCase(
      sourceList: string[],
      otherList: string[],
    ): string[] {
      const otherListLowerCaseAndTrimmed: string[] = otherList.map((x) =>
      // The key is the lower-case and trimmed provider name. The value is the same, but with original casing.
      // We'll return the value so we preserve the casing.
      const missingMap = new Map<string, string>();
      for (const sourceItem of sourceList) {
        const sourceItemLowerCaseAndTrimmed = sourceItem.toLowerCase().trim();
        if (!otherListLowerCaseAndTrimmed.includes(sourceItemLowerCaseAndTrimmed)) {
          missingMap.set(sourceItemLowerCaseAndTrimmed, sourceItem.trim());
      return Array.from(missingMap.values());


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.