Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Nested tenary vs dictionary 2
(version: 0)
Comparing performance of:
Tenary vs Dictionary
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var random = Math.floor(Math.random() * 20);
Tests:
Tenary
const status_ = random === 0 ? -Math.PI: random === 1 ? Math.EPSILON: random === 2 ? 1: random === 3 ? 1: random === 4 ? 1: random === 5 ? 1: random === 6 ? 1: random === 7 ? 1: random === 8 ? 1: random === 9 ? 1: random === 18 ? 5: random === 19 ? 28: random === 20 ? 0: -1;
Dictionary
const test = { 0: -Math.PI, 1: Math.EPSILON, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 18: 5, 19: 28, 20: 0, }; const status2_ = test[random] || -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Tenary
Dictionary
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
gemma2:9b
, generated one year ago):
This benchmark compares two methods for determining a value based on a randomly generated number: **Method 1: Nested Ternary Operator (Tenary)** This method uses a series of nested ternary operators (`condition ? value1 : condition ? value2 : ...`). Each ternary checks if the random number matches a specific value and returns a corresponding result. While concise, this approach can become verbose and difficult to read for many conditions. **Method 2: Dictionary (Object)** This method uses an object (dictionary) where keys are the possible random values and values are the corresponding results. It retrieves the result by looking up the random value as a key in the object. This approach is generally considered more readable and maintainable, especially for larger sets of conditions. **Pros & Cons:** * **Tenary:** * **Pro:** Can be compact for a small number of conditions. * **Con:** Becomes increasingly complex and harder to read as the number of conditions grows. * **Dictionary:** * **Pro:** More readable, maintainable, and scales well for larger numbers of conditions. * **Con:** Slightly less concise than ternary for very simple cases. **Alternatives:** * **Switch Statement:** A more structured approach for multiple conditions, often preferable over nested ternaries for clarity. * **Function Mapping:** If the logic involves transformations rather than direct value assignment, using a function map to apply operations based on the random value could be efficient and readable. Let me know if you'd like to explore any of these alternatives in more detail!
Related benchmarks:
Module vs Math.floor
Math.floor vs Math.trunc
Math.floor vs alternatives 2
Math.floor vs right shift
2's math pow vs shift vs exp random num
Comments
Confirm delete:
Do you really want to delete benchmark?