Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Foreach IF Else VS For General Switch Case
(version: 2)
Comparing performance of:
Foreach IF Else vs For Switch Case
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var GeocodingAddressTypes = { city: 'administrative_area_level_2', district: 'administrative_area_level_3', postalCode: 'postal_code', province: 'administrative_area_level_1', route: 'route', village: 'administrative_area_level_4', }; var geocodingAddress = [ GeocodingAddressTypes.city, GeocodingAddressTypes.district, GeocodingAddressTypes.postalCode, GeocodingAddressTypes.province, GeocodingAddressTypes.route, GeocodingAddressTypes.village, ]; var addressComponents = [ { long_name: "Long Name 1", short_name: "Short Name 1", types: ["administrative_area_level_1"], }, { long_name: "Long Name 1", short_name: "Short Name 1", types: ["administrative_area_level_4", "administrative_area_level_1"], }, { long_name: "Long Name 3", short_name: "Short Name 3", types: ["administrative_area_level_3","administrative_area_level_4", "administrative_area_level_1"], }, { long_name: "Long Name 4", short_name: "Short Name 4", types: ["postal_code"], }, { long_name: "Long Name 5", short_name: "Short Name 5", types: ["route","administrative_area_level_4", "administrative_area_level_2"], } ]; var cityName = ''; var districtName = ''; var postalCode = ''; var province = ''; var route = ''; var village = '';
Tests:
Foreach IF Else
addressComponents.forEach((ac) => { if (ac.types.includes(GeocodingAddressTypes.city)) { cityName = ac.long_name; } else if (ac.types.includes(GeocodingAddressTypes.district)) { districtName = ac.long_name; } else if (ac.types.includes(GeocodingAddressTypes.postalCode)) { postalCode = ac.long_name; } else if (ac.types.includes(GeocodingAddressTypes.province)) { province = ac.long_name; } else if (ac.types.includes(GeocodingAddressTypes.route)) { route = ac.long_name; } else if (ac.types.includes(GeocodingAddressTypes.village)) { village = ac.long_name; } });
For Switch Case
for(var j = 0; j < addressComponents.length; j++) { let addressType = ""; for(var i = 0; i < addressComponents[j].types.length; i++) { if(addressType) break; if(geocodingAddress.includes(addressComponents[j].types[i])) { addressType = addressComponents[j].types[i]; break; } } switch(addressType) { case GeocodingAddressTypes.city: cityName = addressComponents[j].long_name; break; case GeocodingAddressTypes.district: districtName = addressComponents[j].long_name; break; case GeocodingAddressTypes.postalCode: postalCode = addressComponents[j].long_name; break; case GeocodingAddressTypes.province: province = addressComponents[j].long_name; break; case GeocodingAddressTypes.route: route = addressComponents[j].long_name; break; case GeocodingAddressTypes.village: village = addressComponents[j].long_name; break; default: break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Foreach IF Else
For Switch Case
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'll break down the benchmark and its components to explain what's being tested. **Benchmark Overview** The benchmark, titled "Foreach IF Else VS For General Switch Case," compares the performance of three different approaches: 1. **Foreach with If-Else**: This approach uses a `forEach` loop with an `if-else` statement inside it. 2. **For Loop with Switch Statement**: This approach uses a traditional `for` loop and a `switch` statement to filter and assign values. **Test Case 1: Foreach with If-Else** In this test case, the script iterates over the `addressComponents` array using `forEach`. Inside the loop, it checks each component's type against the `GeocodingAddressTypes` object and assigns a value accordingly. The `if-else` statement is used to make decisions based on the component types. **Test Case 2: For Loop with Switch Statement** In this test case, the script uses a traditional `for` loop to iterate over the `addressComponents` array. Inside the loop, it iterates again over the component's types using another `for` loop and breaks when it finds a match. Then, it uses a `switch` statement to filter and assign values based on the matched type. **Options Compared** The two approaches being compared are: * **Foreach with If-Else**: This approach is more concise but may have performance implications due to the overhead of function calls. * **For Loop with Switch Statement**: This approach is more verbose but can be optimized for better performance, especially when dealing with large datasets. **Pros and Cons** **Foreach with If-Else:** Pros: * Concise code * Easy to read and maintain Cons: * Potential performance overhead due to function calls * May not be suitable for very large datasets or performance-critical applications **For Loop with Switch Statement:** Pros: * Can be optimized for better performance, especially when dealing with large datasets * Allows for explicit control over the filtering process Cons: * More verbose code compared to `foreach` with `if-else` * May require more careful consideration of the switch statement's complexity **Other Considerations** Both approaches have trade-offs in terms of readability, maintainability, and performance. The choice between them ultimately depends on the specific requirements of the project and personal preference. **Libraries Used** The benchmark uses the `GeocodingAddressTypes` object, which is not a standard JavaScript library. It appears to be a custom-defined object containing various address types (city, district, postal code, province, route, village). The script also uses the `addressComponents` array, which is populated with sample data. **Special JS Features/Syntax** The benchmark does not explicitly use any special JavaScript features or syntax beyond the ones mentioned above. However, it's worth noting that the use of a custom-defined object like `GeocodingAddressTypes` may be considered an unconventional approach to addressing types in JavaScript.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty 25
null vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty vs bool vs Object.hasOwn
optionally chain me up on valentimes day
zzHasOwn1
Comments
Confirm delete:
Do you really want to delete benchmark?