Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Pick vs Switch
(version: 2)
Tests filtering of object attributes.
Comparing performance of:
testShallowSwitch vs testShallowPick
Created:
9 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.min.js"></script> </head> <body> </body> </html>
Script Preparation code:
var bannerNames = [ "Anniversary", "BeautyExclusives", "Buy2Get1Free", "BuyAndSave", "EarlyAccess", "EarlyAccessSneakPeek", "GiftWithPurchase", "GroomingExclusives", "Holiday", "Lts", "PreOrder", "PriceMatch", "SpecialPurchase" ]; var bannerProps = { Anniversary: null, BeautyExclusives: null, Buy2Get1Free: ["BuyXGetYCopy"], BuyAndSave: [ "BuyAndSave", "BuyAndSaveOffer" ], EarlyAccess: null, EarlyAccessSneakPeek: null, GiftWithPurchase: ["GiftWithPurchase"], GroomingExclusives: null, Holiday: ["Id", "selectedSku"], Lts: ["LTSPromotion"], PreOrder: null, SpecialPurchase: null }; var product = { BuyAndSave: "BuyAndSave", BuyAndSaveOffer: "BuyAndSaveOffer", BuyXGetYCopy: "BuyXGetYCopy", GiftWithPurchase: "GiftWithPurchase", Id: 5, LimitedTimeSavings: "LimitedTimeSavings", LTSPromotion: { EndDate: "EndDate", Name: "Name" }, Price: { AllSkusOnSale: false }, PriceMatching: "PriceMatching", selectedSku: { isAvailable: true, IsAvailableFulfillmentCenter: true } }; function testShallowSwitch() { bannerNames.forEach(function (name) { switch (name) { case "Buy2Get1Free": return { BuyXGetYCopy: product.BuyXGetYCopy }; case "BuyAndSave": return { BuyAndSave: product.BuyAndSave, BuyAndSaveOffer: product.BuyAndSaveOffer, }; case "GiftWithPurchase": return { GiftWithPurchase: product.GiftWithPurchase }; case "Holiday": return { Id: product.Id, selectedSku: product.selectedSku }; case "Lts": return { LTSPromotion: product.LTSPromotion } default: break; } }); } function testShallowPick() { bannerNames.forEach(function (name) { if (!bannerProps[name]) { return null; } var props = _.pick(product, bannerProps[name]); }); }
Tests:
testShallowSwitch
testShallowSwitch();
testShallowPick
testShallowPick();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
testShallowSwitch
testShallowPick
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'm ready to explain the benchmark and its options. **Benchmark Overview** The provided benchmark, `Pick vs Switch`, tests two approaches for filtering object attributes: shallow `switch` statements and shallow `pick` from a library (Lodash). The goal is to determine which approach is faster. **Switch Statement Approach** In this approach, a `switch` statement is used to filter the object attributes. The code uses an array of banner names (`bannerNames`) and iterates over it using a `forEach` loop. For each name, it checks if there's a corresponding property in the `product` object using another `switch` statement. If a match is found, it returns an object with only that property. **Pros and Cons** Pros: * Easy to understand and implement for those familiar with JavaScript * Can be optimized further by adding more cases or using other methods like regular expressions Cons: * Can lead to slower performance due to the multiple `switch` statements * May not scale well if the number of banner names is large **Pick from Lodash Approach** In this approach, a shallow `pick` function from the Lodash library is used to filter the object attributes. The code uses an array of banner names (`bannerNames`) and iterates over it using a `forEach` loop. For each name, it checks if there's a corresponding property in the `product` object using the `_.pick` method. If a match is found, it returns an object with only that property. **Pros and Cons** Pros: * Can be faster than the switch statement approach due to Lodash's optimized implementation * Allows for more flexible filtering options (e.g., selecting specific properties or arrays) Cons: * Requires including the Lodash library in the test environment * May have a higher overhead due to the additional function call **Other Considerations** * The benchmark only tests shallow filtering; deeper filtering would require additional optimizations. * Other factors, like the size of the `product` object and the number of iterations, can impact performance. * The use of Lodash's `pick` method may not be necessary if a custom implementation is already optimized. **Alternatives** If you want to explore other approaches, consider: 1. Using `Object.keys()` and `Array.prototype.forEach()` for filtering 2. Implementing a custom `filter()` function using bitwise operators (e.g., `if ((product[bannerName] & 0x7fffffff) !== 0)` ) 3. Utilizing modern JavaScript features like `Object.fromEntries()` or destructuring assignment
Related benchmarks:
js includes inside filter
not includes vs. not equal to
two condition if vs includes compare
Difference between lists vs filtering on property
indexOf vs multiple === vs object selection
Comments
Confirm delete:
Do you really want to delete benchmark?