exapsy

Untitled

Oct 18th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. const searchEntries = [];
  2. const excludeTags = [];
  3. const 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. console.log('Populating data...');
  30. for (let i = 0; i < 1000; i++) {
  31. const se = {
  32. isOpen: () => true,
  33. getOffers: () => [
  34. {
  35. id: i,
  36. title: 'title',
  37. description: 'description',
  38. tag: 'tag',
  39. metric_description: 'metric_description',
  40. size_info: 'size_info',
  41. full_price: 'full_price',
  42. mode: 'mode',
  43. price: 'price',
  44. logo: 'logo',
  45. restaurant_id: i,
  46. badge: 'badge',
  47. images: {
  48. menu: null,
  49. menu_bh: null
  50. }
  51. }
  52. ],
  53. getId: () => i
  54. };
  55. searchEntries.push(se);
  56. excludeTags.push('tag' + i);
  57. terms.push('title' + i);
  58. }
  59.  
  60.  
  61. console.time('Array map vanilla');
  62. searchEntries.forEach((se, sid) => {
  63. if (!se.isOpen()) {return;}
  64. const offers = se.getOffers([], excludeTags);
  65. offers.forEach((o) => {
  66. terms.forEach((t) => {
  67. if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
  68. const offer = {
  69. id:o.id,
  70. title:o.title,
  71. description:o.description,
  72. tag:o.tag,
  73. metric_description:o.metric_description,
  74. size_info:o.size_info,
  75. full_price:o.full_price,
  76. mode:o.mode,
  77. price:o.price,
  78. logo:o.logo,
  79. restaurant_id: se.getId(),
  80. badge: generateOfferBadgeSimple(opts, o),
  81. images: {
  82. menu: null,
  83. menu_bh: null
  84. }
  85. };
  86. validOffers.push(offer);
  87. validRestaurantsIds.push(sid);
  88. }
  89. });
  90. });
  91. });
  92. console.timeEnd('Array map vanilla');
  93.  
  94. // reset
  95. validOffers.length = 0;
  96. validRestaurantsIds.length = 0;
  97.  
  98. console.time('Array map vanilla2');
  99. for (let sid = 0; sid < searchEntries.length; sid++) {
  100. const se = searchEntries[sid];
  101. if (!se.isOpen()) {
  102. continue;
  103. }
  104.  
  105. const offers = se.getOffers([], excludeTags);
  106. for (let i = 0; i < offers.length; i++) {
  107. const o = offers[i];
  108. for (let j = 0; j < terms.length; j++) {
  109. const t = terms[j];
  110. if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
  111. const offer = {
  112. id: o.id,
  113. title: o.title,
  114. description: o.description,
  115. tag: o.tag,
  116. metric_description: o.metric_description,
  117. size_info: o.size_info,
  118. full_price: o.full_price,
  119. mode: o.mode,
  120. price: o.price,
  121. logo: o.logo,
  122. restaurant_id: se.getId(),
  123. badge: generateOfferBadgeSimple(opts, o),
  124. images: {
  125. menu: null,
  126. menu_bh: null
  127. }
  128. };
  129. validOffers.push(offer);
  130. validRestaurantsIds.push(sid);
  131. }
  132. }
  133. }
  134. }
  135. console.timeEnd('Array map vanilla2');
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment