Algorithmes de recherche de recettes en JavaScript (v3)

Revision 3 of this benchmark created on


Setup

const recipes = [
  {
    name: "Recette 1",
    description: "Description de la recette 1",
    ingredients: [
      { ingredient: "Ingrédient 1" },
      { ingredient: "Ingrédient 2" }
    ]
  },
  {
    name: "Recette 2",
    description: "Description de la recette 2",
    ingredients: [
      { ingredient: "Ingrédient 3" },
      { ingredient: "Ingrédient 4" }
    ]
  }
];

const searchText = "recette";

Test runner

Ready to run.

Testing in
TestOps/sec
Algorithme de recherche avec des boucles for
const text = searchText.trim().toLowerCase();

  const filteredRecipes = [];
  for (let i = 0; i < recipes.length; i++) {
    const recipe = recipes[i];
    const verifyName = recipe.name.toLowerCase().includes(text);
    const verifyDescription = recipe.description.toLowerCase().includes(text);
  
    let verifyIngredient = false;
    for (let a = 0; a < recipe.ingredients.length; a++) {
      const item = recipe.ingredients[a];
      if (item.ingredient.toLowerCase().includes(text)) {
        verifyIngredient = true;
        break;
      }
  }
     if (verifyName || verifyDescription || verifyIngredient) {
           filteredRecipes.push(recipe);
         }
}
ready
Algorithme de recherche avec la méthode filter
 const text = searchText.trim().toLowerCase();

const filteredSearch = recipes.filter((recipe) => {
    const nameRecipe = recipe.name.toLowerCase().includes(text);
    const descRecipe = recipe.description.toLowerCase().includes(text);
    const ingredientRecipe = recipe.ingredients.some((item) => item.ingredient.toLowerCase().includes(text));

    return nameRecipe || descRecipe || ingredientRecipe;
  });
ready

Revisions

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