SegmentFilter category list processing functions

Benchmark created on


Preparation HTML

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

Setup

// Lodash sortBy - use same as app, or native for comparison
function sortByLabel(arr) {
  return arr.slice().sort((a, b) => (a.label || "").toLowerCase().localeCompare((b.label || "").toLowerCase()));
}

// Mock options with label (like segment filter options)
function makeOptions(n, prefix) {
  const out = [];
  for (let i = 0; i < n; i++) {
    out.push({ value: "opt_" + i, label: (prefix || "Option ") + " " + i, category: "test" });
  }
  return out;
}

// formatConditionsInSubGroups - simplified (no getSurveyType/surveyResponsesRanges)
const CONDITION_LABEL = {
  user_events: "Events",
  product_features: "Feature Engagement",
  "imported_property-company": "Company properties",
  "imported_property-user": "User properties",
  default_property: "Default Properties",
  surveys: "Microsurvey events",
  campaigns: "Tours",
  lists: "Launchers",
};

function formatConditionsInSubGroups(currentConditions) {
  const conditions = currentConditions.slice();
  const formatedConditionsInSubGroups = [
    ...new Set(conditions.map((c) => `${c.category}${c.propertyType ? "-" + c.propertyType : ""}`)),
  ].map((condition) => {
    const category = condition.split("-")[0];
    const propertyType = condition.split("-")[1];
    return {
      label: CONDITION_LABEL[condition] || condition,
      category,
      propertyType,
      options: [],
    };
  });
  currentConditions.forEach((condition) => {
    formatedConditionsInSubGroups.forEach((formatedCondition) => {
      if (condition?.propertyType === formatedCondition.propertyType) {
        formatedCondition.options.push(condition);
        return;
      }
      if (condition.category === formatedCondition.category) {
        formatedCondition.options.push(condition);
      }
    });
  });
  return formatedConditionsInSubGroups;
}

function getSortedUserEvents(newCustomEvents, userEventNames) {
  const combined = [...newCustomEvents, ...userEventNames];
  const seen = new Set();
  const deduped = combined.filter((e) => {
    if (seen.has(e.id)) return false;
    seen.add(e.id);
    return true;
  });
  const enabled = deduped.filter((e) => !e.isDisabled);
  const disabled = deduped.filter((e) => !!e.isDisabled);
  const byLabel = (a, b) => (a.label || "").toLowerCase().localeCompare((b.label || "").toLowerCase());
  return [...enabled.sort(byLabel), ...disabled.sort(byLabel)];
}

// Data sizes similar to your screenshot (adjust as needed)
var userProperties = makeOptions(609, "User property");
var companyProperties = makeOptions(116, "Company property");
var campaigns = makeOptions(1234, "Tour");
var surveys = makeOptions(996, "Survey");
var launchers = makeOptions(676, "Launcher");
var embeds = makeOptions(965, "Embed");
var eventNames = makeOptions(143, "Event").map((o, i) => ({ ...o, id: "ev_" + i, isDisabled: i % 10 === 0 }));
var allConditions = [
  ...userProperties.map((o) => ({ ...o, category: "imported_property", propertyType: "user" })),
  ...companyProperties.map((o) => ({ ...o, category: "imported_property", propertyType: "company" })),
  ...campaigns.map((o) => ({ ...o, category: "campaigns" })),
  ...surveys.map((o) => ({ ...o, category: "surveys" })),
  ...launchers.map((o) => ({ ...o, category: "lists" })),
];

Test runner

Ready to run.

Testing in
TestOps/sec
Test case 1: sortBy userProperties (609 items)
var result = _.sortBy(userProperties, function (o) { return (o.label || "").toLowerCase(); });
return result.length;
ready
Test case 2: sortBy campaigns (1234 items)
var result = _.sortBy(campaigns, function (o) { return (o.label || "").toLowerCase(); });
return result.length;
ready
Test case 3: formatConditionsInSubGroups (full mixed list)
var result = formatConditionsInSubGroups(allConditions);
return result.length;
ready
Test case 4: getSortedUserEvents (event names)
var result = getSortedUserEvents([], eventNames);
return result.length;
ready
Test case 5: sortBy all categories (combined cost)
_.sortBy(userProperties, function (o) { return (o.label || "").toLowerCase(); });
_.sortBy(companyProperties, function (o) { return (o.label || "").toLowerCase(); });
_.sortBy(campaigns, function (o) { return (o.label || "").toLowerCase(); });
_.sortBy(surveys, function (o) { return (o.label || "").toLowerCase(); });
_.sortBy(launchers, function (o) { return (o.label || "").toLowerCase(); });
_.sortBy(embeds, function (o) { return (o.label || "").toLowerCase(); });
return 1;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.