From 0e5c6181b947aa225896078ce9277e06a0463248 Mon Sep 17 00:00:00 2001 From: Przemek Wiech Date: Wed, 20 Mar 2019 00:07:16 +0100 Subject: [PATCH] Speed up file processing by not initializing the comparator multiple times. --- src/gedcom_util.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/gedcom_util.ts b/src/gedcom_util.ts index f2af7fb..2a0d5d5 100644 --- a/src/gedcom_util.ts +++ b/src/gedcom_util.ts @@ -84,11 +84,14 @@ function birthDatesComparator(gedcom: JsonGedcomData) { * Sorts children by birth date in the given family. * Does not modify the input objects. */ -function sortFamilyChildren(fam: JsonFam, gedcom: JsonGedcomData): JsonFam { +function sortFamilyChildren( + fam: JsonFam, + comparator: (id1: string, id2: string) => number, +): JsonFam { if (!fam.children) { return fam; } - const newChildren = fam.children.sort(birthDatesComparator(gedcom)); + const newChildren = fam.children.sort(comparator); return Object.assign({}, fam, {children: newChildren}); } @@ -97,7 +100,8 @@ function sortFamilyChildren(fam: JsonFam, gedcom: JsonGedcomData): JsonFam { * Does not modify the input object. */ function sortChildren(gedcom: JsonGedcomData): JsonGedcomData { - const newFams = gedcom.fams.map((fam) => sortFamilyChildren(fam, gedcom)); + const comparator = birthDatesComparator(gedcom); + const newFams = gedcom.fams.map((fam) => sortFamilyChildren(fam, comparator)); return Object.assign({}, gedcom, {fams: newFams}); }