exapsy

Untitled

Oct 18th, 2024
209
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 (let sid = 0; sid < searchEntries.length; sid++) {
  124.     const se = searchEntries[sid];
  125.     if (!se.isOpen()) {
  126.       continue;
  127.     }
  128.  
  129.     const offers = se.getOffers([], excludeTags);
  130.     for (let i = 0; i < offers.length; i++) {
  131.       const o = offers[i];
  132.       for (let j = 0; j < terms.length; j++) {
  133.         const t = terms[j];
  134.         if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
  135.           const offer = {
  136.             id: o.id,
  137.             title: o.title,
  138.             description: o.description,
  139.             tag: o.tag,
  140.             metric_description: o.metric_description,
  141.             size_info: o.size_info,
  142.             full_price: o.full_price,
  143.             mode: o.mode,
  144.             price: o.price,
  145.             logo: o.logo,
  146.             restaurant_id: se.getId(),
  147.             badge: generateOfferBadgeSimple(opts, o),
  148.             images: {
  149.               menu: null,
  150.               menu_bh: null
  151.             }
  152.           };
  153.           validOffers.push(offer);
  154.           validRestaurantsIds.push(sid);
  155.         }
  156.       }
  157.     }
  158.   }
  159. };
  160.  
  161.  
  162. console.log('Benchmarking...');
  163. const iterations = 10;
  164.  
  165. console.log('Benchmarking forEachWay');
  166. const time1 = benchmarkFunction(forEachWay, [], iterations);
  167. console.log('Benchmarking forWay');
  168. const time2 = benchmarkFunction(forWay, [], iterations);
  169.  
  170. console.log(`forEachWay: ${time1}ms`);
  171. console.log(`forWay: ${time2}ms`);
  172.  
Advertisement
Add Comment
Please, Sign In to add comment