map vs reduce (v2)

Revision 2 of this benchmark created on


Description

map vs reduce

Setup

const searchData = [
  { title: "Apple iPhone 15", description: "Latest model with improved camera" },
  { title: "Samsung Galaxy S23", description: "High performance Android smartphone" },
  { title: "Google Pixel 8", description: "Pure Android experience with AI features" },
  { title: "OnePlus 12", description: "Fast charging and smooth display" },
  { title: "Sony Xperia 1", description: "Professional camera technology in a phone" },
  { title: "Nokia X20", description: "Durable phone with long battery life" },
  { title: "Motorola Edge", description: "Edge display and strong performance" },
  { title: "Xiaomi Mi 14", description: "Affordable phone with flagship specs" },
  { title: "Huawei P60", description: "Excellent camera and design" },
  { title: "Oppo Find X6", description: "Innovative design and performance" },
    { title: "Apple iPhone 15", description: "Latest model with improved camera" },
  { title: "Samsung Galaxy S23", description: "High performance Android smartphone" },
  { title: "Google Pixel 8", description: "Pure Android experience with AI features" },
  { title: "OnePlus 12", description: "Fast charging and smooth display" },
  { title: "Sony Xperia 1", description: "Professional camera technology in a phone" },
  { title: "Nokia X20", description: "Durable phone with long battery life" },
  { title: "Motorola Edge", description: "Edge display and strong performance" },
  { title: "Xiaomi Mi 14", description: "Affordable phone with flagship specs" },
  { title: "Huawei P60", description: "Excellent camera and design" },
  { title: "Oppo Find X6", description: "Innovative design and performance" },
    { title: "Apple iPhone 15", description: "Latest model with improved camera" },
  { title: "Samsung Galaxy S23", description: "High performance Android smartphone" },
  { title: "Google Pixel 8", description: "Pure Android experience with AI features" },
  { title: "OnePlus 12", description: "Fast charging and smooth display" },
  { title: "Sony Xperia 1", description: "Professional camera technology in a phone" },
  { title: "Nokia X20", description: "Durable phone with long battery life" },
  { title: "Motorola Edge", description: "Edge display and strong performance" },
  { title: "Xiaomi Mi 14", description: "Affordable phone with flagship specs" },
  { title: "Huawei P60", description: "Excellent camera and design" },
  { title: "Oppo Find X6", description: "Innovative design and performance" },
    { title: "Apple iPhone 15", description: "Latest model with improved camera" },
  { title: "Samsung Galaxy S23", description: "High performance Android smartphone" },
  { title: "Google Pixel 8", description: "Pure Android experience with AI features" },
  { title: "OnePlus 12", description: "Fast charging and smooth display" },
  { title: "Sony Xperia 1", description: "Professional camera technology in a phone" },
  { title: "Nokia X20", description: "Durable phone with long battery life" },
  { title: "Motorola Edge", description: "Edge display and strong performance" },
  { title: "Xiaomi Mi 14", description: "Affordable phone with flagship specs" },
  { title: "Huawei P60", description: "Excellent camera and design" },
  { title: "Oppo Find X6", description: "Innovative design and performance" },
];

const query = { value: "item 5" };
let filteredResults = [];
let searchExecuted = false;

function highlightMatch(text, term) {
  return text.replace(new RegExp(`(${term})`, "gi"), "[$1]");
}

Test runner

Ready to run.

Testing in
TestOps/sec
мапим
function handleInputMap() {
  searchExecuted = false;

  const term = query.value.trim().toLowerCase();
  if (!term) {
    filteredResults = [];
    return;
  }

  filteredResults = searchData
    .filter(
      item =>
        item.title.toLowerCase().includes(term) ||
        (item.description && item.description.toLowerCase().includes(term))
    )
    .map(item => ({
      ...item,
      title: highlightMatch(item.title, term),
      description: item.description ? highlightMatch(item.description, term) : undefined
    }));

  searchExecuted = true;
}
ready
редюсим
function handleInputReduce() {
  searchExecuted = false;

  const term = query.value.trim().toLowerCase();
  if (!term) {
    filteredResults = [];
    return;
  }

  filteredResults = searchData.reduce((acc, item) => {
    const titleMatch = item.title.toLowerCase().includes(term);
    const descriptionMatch = item.description && item.description.toLowerCase().includes(term);

    if (titleMatch || descriptionMatch) {
      acc.push({
        ...item,
        title: highlightMatch(item.title, term),
        description: item.description ? highlightMatch(item.description, term) : ""
      });
    }

    return acc;
  }, []);

  searchExecuted = true;
}
ready

Revisions

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