ingredient count

Benchmark created by LJ_1102 on


Preparation HTML

<script src="https://underscorejs.org/underscore-min.js"></script>

Setup

var products = [{
      name: "Sonoma",
      ingredients: ["artichoke", "sundried tomatoes", "mushrooms"],
      containsNuts: false
    }, {
      name: "Pizza Primavera",
      ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"],
      containsNuts: false
    }, {
      name: "South Of The Border",
      ingredients: ["black beans", "jalapenos", "mushrooms"],
      containsNuts: false
    }, {
      name: "Blue Moon",
      ingredients: ["blue cheese", "garlic", "walnuts"],
      containsNuts: true
    }, {
      name: "Taste Of Athens",
      ingredients: ["spinach", "kalamata olives", "sesame seeds"],
      containsNuts: true
    }];
    
    function ic(p,o,i,b,g) {
          i|=0;g|=0;
          if(!b&&p&&!p.ingredients)
            while((g=ic(p[g].ingredients,o,0,g+1))&&p[g]);
          else {
                o[p[i]]=(o[p[i]]|0)+1;
            return p[i+1]?ic(p,o,i+1,b):b;
          }
        }

Test runner

Ready to run.

Testing in
TestOps/sec
underscore
var ingredientCount = {};
_.flatten(products.map(function(x) {
  return x.ingredients;
})).map(function(y) {
  return ingredientCount[y] = (ingredientCount[y] || 0) + 1;
});
ready
recursion
var ingredientCount = {};
ic(products, ingredientCount);
ready

Revisions

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