Map & Sort (current) VS Reduce VS Lexicographical Sort (v3)

Revision 3 of this benchmark created on


Setup

var arr = [
    "932216930",
    "932218821",
    "932216031",
    "932217953",
    "T1732098502943_-508874983",
    "932214202",
    "932213303",
    "1276997668",
    "932212342",
    "932215969",
    "932214233",
    "932213148",
    "932217891",
    "932217085",
    "932212094",
    "932214977",
    "932214047",
    "932218883",
    "T1732097981792_-545120851",
    "T1732098243456_687905557",
    "1276997513",
    "932212218",
    "932214016",
    "932218077",
    "932214264",
    "932219782",
    "932215938",
    "932216062",
    "932217860",
    "T1732098403933_-114683095",
    "T1732098545834_-741268739",
    "932219906",
    "T1732098582051_1512525735",
    "932215039",
    "932212373",
    "932215132",
    "932219999",
    "1276997575",
    "932216155",
    "932214078",
    "932216093",
    "932217922",
    "932215101",
    "932217178",
    "932214295",
    "932218976",
    "932213055",
    "932216124",
    "932215256",
    "932217147",
    "932213210",
    "932219968",
    "932214171",
    "932214140",
    "T1732098004300_-1659749212",
    "T1732098345724_-638364226",
    "1276997637",
    "T1732098501528_-206656958",
    "932219100",
    "932219069",
    "932219937",
    "932213086",
    "932217023",
    "932213179",
    "1276997606",
    "T1732098602260_487819698",
    "932216217",
    "T1732098004889_1482163197",
    "T1732098601819_1964243017",
    "932218945",
    "T1732098577010_-1224298579",
    "932212311",
    "932218852",
    "T1732098405283_683650879",
    "932218046",
    "T1732098529440_-1380707450",
    "932212249",
    "932217984",
    "932217054",
    "932219844",
    "T1732098004888_1473844466",
    "932218015",
    "T1732098271239_1918759769",
    "1276997482",
    "T1732098590755_-151003662",
    "932216899",
    "932212156",
    "932215008",
    "T1732098398387_696690858",
    "932218139",
    "932219813",
    "932219007",
    "932212125",
    "932213241",
    "932215225",
    "T1732098003760_1556536553",
    "932217116",
    "932214109",
    "932212187",
    "932218108",
    "932216186",
    "932216992",
    "T1732098412762_-1942480529",
    "932219038",
    "T1732098533453_-526841989",
    "932212280",
    "932216961",
    "T1732098354897_696690858",
    "932215163"
]

function getBackwardCompatibleKey(hash, timestamp) {
  if (!timestamp) {
    return hash;
  }

  return `T${timestamp}_${hash}`;
}

function parseKey(key) {
  if (!key) {
    return { hash: '', timestamp: 0 };
  }

  if (key.startsWith('T')) {
    const [timestamp, hash] = key.split('_');

    if (!hash) {
      return { hash: '', timestamp: 0 };
    }

    const numericTimestamp = parseInt(timestamp.slice(1));
    if (isNaN(numericTimestamp)) {
      return { hash, timestamp: 0 };
    }

    return { hash, timestamp: numericTimestamp };
  }

  // Backward compatibility layer. Old entries don't have a timestamp.
  return { hash: key, timestamp: 0 };
}

function findOldestKey(keys) {
  const parsedKeys = keys.map(parseKey);

  parsedKeys.sort((first, second) => first.timestamp - second.timestamp);
  const oldest = parsedKeys[0];
  return getBackwardCompatibleKey(oldest.hash, oldest.timestamp);
}

function findOldestKeyReduce(keys) {
  const oldest = keys.reduce(
    (oldest, current) => {
      const parsedCurrentKey = parseKey(current);

      return parsedCurrentKey.timestamp < oldest.timestamp
        ? oldest
        : parsedCurrentKey;
    },
    { hash: '', timestamp: 0 }
  );

  return getBackwardCompatibleKey(oldest.hash, oldest.timestamp);
}

function findOldestKeySort(keys) {
  const sorted = keys.toSorted();
  const oldest = parseKey(sorted[0]);
  return getBackwardCompatibleKey(oldest.hash, oldest.timestamp);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Map & Sort
findOldestKey(arr)
ready
Reduce
findOldestKeyReduce(arr)
ready
Lexicographical Sort
findOldestKeySort(arr)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.