Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let searchEntries = [];
- let excludeTags = [];
- let terms = [];
- const validOffers = [];
- const validRestaurantsIds = [];
- const opts = {
- showBadge: true,
- showPrice: true,
- showPriceDiscount: true,
- showPriceFull: true,
- showPriceMode: true,
- showPriceSize: true,
- showPriceMetric: true,
- showTitle: true,
- showDescription: true,
- showLogo: true,
- showRestaurantId: true,
- showImages: true
- };
- const generateOfferBadgeSimple = (opts, offer) => {
- let badge = '';
- if (opts.showBadge) {
- badge = offer.badge;
- }
- return badge;
- };
- function benchmarkFunction(func, args, iterations = 100) {
- populateData();
- console.log(`benchmarking(${func.name}, iterations: ${iterations})`);
- console.log('\twarming up');
- // Warm-up run
- for (let i = 0; i < 10; i++) {
- // @ts-expect-error unknown
- func(...args);
- }
- console.log('\tbenchmarking');
- const start = performance.now();
- for (let i = 0; i < iterations; i++) {
- // @ts-expect-error unknown
- func(...args);
- }
- const end = performance.now();
- console.log('\tfinished !\n');
- return end - start;
- }
- const populateData = () => {
- console.log('Populating data...');
- searchEntries = [];
- excludeTags = [];
- terms = [];
- const se = (i) => ({
- isOpen: () => true,
- getOffers: () => [
- {
- id: i,
- title: 'title',
- description: 'description',
- tag: 'tag',
- metric_description: 'metric_description',
- size_info: 'size_info',
- full_price: 'full_price',
- mode: 'mode',
- price: 'price',
- logo: 'logo',
- restaurant_id: i,
- badge: 'badge',
- images: {
- menu: null,
- menu_bh: null
- }
- }
- ],
- getId: () => i
- });
- for (let i = 0; i < 10000; i++) {
- const seInstance = se(i);
- searchEntries.push(seInstance);
- excludeTags.push('tag' + i);
- terms.push('title' + i);
- }
- };
- const forEachWay = () => {
- searchEntries.forEach((se, sid) => {
- if (!se.isOpen()) {return;}
- const offers = se.getOffers([], excludeTags);
- offers.forEach((o) => {
- terms.forEach((t) => {
- if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
- const offer = {
- id:o.id,
- title:o.title,
- description:o.description,
- tag:o.tag,
- metric_description:o.metric_description,
- size_info:o.size_info,
- full_price:o.full_price,
- mode:o.mode,
- price:o.price,
- logo:o.logo,
- restaurant_id: se.getId(),
- badge: generateOfferBadgeSimple(opts, o),
- images: {
- menu: null,
- menu_bh: null
- }
- };
- validOffers.push(offer);
- validRestaurantsIds.push(sid);
- }
- });
- });
- });
- };
- const forWay = () => {
- for (let sid = 0; sid < searchEntries.length; sid++) {
- const se = searchEntries[sid];
- if (!se.isOpen()) {
- continue;
- }
- const offers = se.getOffers([], excludeTags);
- for (let i = 0; i < offers.length; i++) {
- const o = offers[i];
- for (let j = 0; j < terms.length; j++) {
- const t = terms[j];
- if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
- const offer = {
- id: o.id,
- title: o.title,
- description: o.description,
- tag: o.tag,
- metric_description: o.metric_description,
- size_info: o.size_info,
- full_price: o.full_price,
- mode: o.mode,
- price: o.price,
- logo: o.logo,
- restaurant_id: se.getId(),
- badge: generateOfferBadgeSimple(opts, o),
- images: {
- menu: null,
- menu_bh: null
- }
- };
- validOffers.push(offer);
- validRestaurantsIds.push(sid);
- }
- }
- }
- }
- };
- console.log('Benchmarking...');
- const iterations = 10;
- console.log('Benchmarking forEachWay');
- const time1 = benchmarkFunction(forEachWay, [], iterations);
- console.log('Benchmarking forWay');
- const time2 = benchmarkFunction(forWay, [], iterations);
- console.log(`forEachWay: ${time1}ms`);
- console.log(`forWay: ${time2}ms`);
Add Comment
Please, Sign In to add comment