exapsy

Untitled

Oct 18th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let searchEntries = [];
  2. let excludeTags = [];
  3. let terms = [];
  4. const validOffers = [];
  5. const validRestaurantsIds = [];
  6. const opts = {
  7.   showBadge: true,
  8.   showPrice: true,
  9.   showPriceDiscount: true,
  10.   showPriceFull: true,
  11.   showPriceMode: true,
  12.   showPriceSize: true,
  13.   showPriceMetric: true,
  14.   showTitle: true,
  15.   showDescription: true,
  16.   showLogo: true,
  17.   showRestaurantId: true,
  18.   showImages: true
  19. };
  20.  
  21. const generateOfferBadgeSimple = (opts, offer) => {
  22.   let badge = '';
  23.   if (opts.showBadge) {
  24.     badge = offer.badge;
  25.   }
  26.   return badge;
  27. };
  28.  
  29. function benchmarkFunction(func, args, iterations = 100) {
  30.   populateData();
  31.  
  32.   console.log(`benchmarking(${func.name}, iterations: ${iterations})`);
  33.   console.log('\twarming up');
  34.   // Warm-up run
  35.   for (let i = 0; i < 10; i++) {
  36.     // @ts-expect-error unknown
  37.     func(...args);
  38.   }
  39.  
  40.   console.log('\tbenchmarking');
  41.   const start = performance.now();
  42.   for (let i = 0; i < iterations; i++) {
  43.     // @ts-expect-error unknown
  44.     func(...args);
  45.   }
  46.   const end = performance.now();
  47.   console.log('\tfinished !\n');
  48.   return end - start;
  49. }
  50.  
  51. const populateData = () => {
  52.   console.log('Populating data...');
  53.   searchEntries = [];
  54.   excludeTags = [];
  55.   terms = [];
  56.  
  57.   const se = (i) => ({
  58.     isOpen: () => true,
  59.     getOffers: () => [
  60.       {
  61.         id: i,
  62.         title: 'title',
  63.         description: 'description',
  64.         tag: 'tag',
  65.         metric_description: 'metric_description',
  66.         size_info: 'size_info',
  67.         full_price: 'full_price',
  68.         mode: 'mode',
  69.         price: 'price',
  70.         logo: 'logo',
  71.         restaurant_id: i,
  72.         badge: 'badge',
  73.         images: {
  74.           menu: null,
  75.           menu_bh: null
  76.         }
  77.       }
  78.     ],
  79.     getId: () => i
  80.   });
  81.   for (let i = 0; i < 10000; i++) {
  82.     const seInstance = se(i);
  83.     searchEntries.push(seInstance);
  84.     excludeTags.push('tag' + i);
  85.     terms.push('title' + i);
  86.   }
  87. };
  88.  
  89. const forEachWay = () => {
  90.   searchEntries.forEach((se, sid) => {
  91.     if (!se.isOpen()) {return;}
  92.     const offers = se.getOffers([], excludeTags);
  93.     offers.forEach((o) => {
  94.       terms.forEach((t) => {
  95.         if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
  96.           const offer = {
  97.             id:o.id,
  98.             title:o.title,
  99.             description:o.description,
  100.             tag:o.tag,
  101.             metric_description:o.metric_description,
  102.             size_info:o.size_info,
  103.             full_price:o.full_price,
  104.             mode:o.mode,
  105.             price:o.price,
  106.             logo:o.logo,
  107.             restaurant_id: se.getId(),
  108.             badge: generateOfferBadgeSimple(opts, o),
  109.             images: {
  110.               menu: null,
  111.               menu_bh: null
  112.             }
  113.           };
  114.           validOffers.push(offer);
  115.           validRestaurantsIds.push(sid);
  116.         }
  117.       });
  118.     });
  119.   });
  120. };
  121.  
  122. const forWay = () => {
  123.   for (const se of searchEntries) {
  124.     if (!se.isOpen()) {
  125.       continue;
  126.     }
  127.  
  128.     const offers = se.getOffers([], excludeTags);
  129.     for (const o of offers) {
  130.       for (const t of terms) {
  131.         if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
  132.           const offer = {
  133.             id: o.id,
  134.             title: o.title,
  135.             description: o.description,
  136.             tag: o.tag,
  137.             metric_description: o.metric_description,
  138.             size_info: o.size_info,
  139.             full_price: o.full_price,
  140.             mode: o.mode,
  141.             price: o.price,
  142.             logo: o.logo,
  143.             restaurant_id: se.getId(),
  144.             badge: generateOfferBadgeSimple(opts, o),
  145.             images: {
  146.               menu: null,
  147.               menu_bh: null
  148.             }
  149.           };
  150.           validOffers.push(offer);
  151.           validRestaurantsIds.push(sid);
  152.         }
  153.       }
  154.     }
  155.   }
  156. };
  157.  
  158.  
  159. console.log('Benchmarking...');
  160. const iterations = 100;
  161.  
  162. const time1 = benchmarkFunction(forEachWay, [], iterations);
  163. const time2 = benchmarkFunction(forWay, [], iterations);
  164.  
  165. console.log(`forEachWay: ${time1}ms`);
  166. console.log(`forWay: ${time2}ms`);
  167.  
Advertisement
Add Comment
Please, Sign In to add comment