ptmdmusique

Remove Item based on given IDs list

Jan 25th, 2021
1,359
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import faker from "faker";
  2. import { generateAlert } from "../alert";
  3. import { generateRandomArray } from "../common/utils";
  4. import { logError, logInfo, logWarning } from "../log/log";
  5.  
  6. const runTestSuite = (numAlert = 10000) => {
  7.   // Original data list
  8.   const alertList = generateRandomArray(generateAlert, numAlert);
  9.  
  10.   // Randomly pick out elements to remove from the list
  11.   const removeIdList = alertList
  12.     .filter(faker.random.boolean)
  13.     .map((entry) => entry.alertId);
  14.  
  15.   // Append some more randomly generated IDs for stress test
  16.   const numAddition = faker.random.number(numAlert * 0.8);
  17.   for (let i = 0; i < numAddition; i++) {
  18.     removeIdList.push(faker.random.uuid());
  19.   }
  20.  
  21.   logInfo("Alert list", `${alertList.length} entries`);
  22.   logInfo("Remove id list", `${removeIdList.length} entries`);
  23.  
  24.   // Naive algo
  25.   const runNaiveAlgo = () => {
  26.     // Non destructive action
  27.     alertList.filter((entry) => !removeIdList.includes(entry.alertId));
  28.   };
  29.  
  30.   // Naive algo
  31.   const runMapAlgo = () => {
  32.     const toRemoveMap: Record<string, boolean> = {};
  33.     removeIdList.forEach((id) => (toRemoveMap[id] = true));
  34.  
  35.     // Non destructive action
  36.     alertList.filter((entry) => !toRemoveMap[entry.alertId]);
  37.     Object.entries(alertList);
  38.     Object.entries(removeIdList);
  39.   };
  40.  
  41.   let t0 = Date.now();
  42.   runMapAlgo();
  43.   let t1 = Date.now();
  44.   logWarning("Map", `${t1 - t0} milliseconds`);
  45.  
  46.   t0 = Date.now();
  47.   runNaiveAlgo();
  48.   t1 = Date.now();
  49.   logError("Naive", `${t1 - t0} milliseconds`);
  50. };
  51.  
  52. const testAmountList = [10, 100, 1000, 5000, 10000, 50000, 100000];
  53. for (let i = 0; i < testAmountList.length; i++) {
  54.   runTestSuite(testAmountList[i]);
  55. }
  56.  
RAW Paste Data