Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Conditionals (if-else, ternary, switch, object-function, object-plain) with last evaluation v3 (with obj-get!)
(version: 2)
Stress testing ideas for possible and fastest condition searching
Comparing performance of:
Object-Function vs Object-plain vs If-Else vs Ternary vs Switch vs Object-Get
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
Object-Function
(({ 'object'(){return 'object'}, 'boolean'(){return 'boolean'}, 'function'(){return 'function'}, 'string'(){return 'string'} //the condition used to filter an object above })[typeof 'string']||(()=>'undefined'))()
Object-plain
({ 'object':'object', 'boolean':'boolean', 'function':'function', 'string':'string' //the condition used to filter an object above })[typeof 'string']||'undefined'
If-Else
if(typeof 'string'==='object'){return 'object'} else if(typeof 'string'==='boolean'){return 'boolean'} else if(typeof 'string'==='function'){return 'function'} else if(typeof 'string'==='string'){return 'string'} else { return 'undefined' }
Ternary
typeof 'string'==='object'?'object': typeof 'string'==='boolean'?'boolean': typeof 'string'==='function'?'function': typeof 'string'==='string'?'string': 'undefined';
Switch
switch (typeof 'string') { case 'object': return 'object'; break; case 'boolean': return 'boolean' break; case 'function': return 'function'; break; case 'string': return 'string'; break; default: 'undefined'; }
Object-Get
({ get 'object'(){return 'object'}, get 'boolean'(){return 'boolean'}, get 'function'(){return 'function'}, get 'string'(){return 'string'} //the condition used to filter an object above })[typeof 'string']||'undefined'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Object-Function
Object-plain
If-Else
Ternary
Switch
Object-Get
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):
Let's break down the provided benchmark definition and test cases to understand what's being tested. **Overview** The benchmark is designed to stress-test different approaches for condition searching in JavaScript, including various data types (object, boolean, function, string). The goal is to identify the fastest approach. **Options Compared** Four options are compared: 1. **Object-Function**: Uses an object with methods that return a specific value based on the type of the input string. 2. **Object-Plain**: Uses an object with properties that return a specific value based on the type of the input string. 3. **If-Else**: Uses traditional if-else statements to check the type of the input string. 4. **Ternary**: Uses ternary operators to concisely express the same logic as If-Else. 5. **Switch**: Uses switch statements to check the type of the input string. 6. **Object-Get**: Uses an object with getter methods that return a specific value based on the type of the input string. **Pros and Cons** Here's a brief summary of each approach: 1. **Object-Function**: Pros: concise, readable; Cons: might be slower due to method calls. 2. **Object-Plain**: Pros: similar performance to Object-Function; Cons: less readable. 3. **If-Else**: Pros: widely supported, easy to understand; Cons: can be slower and more verbose. 4. **Ternary**: Pros: concise, fast; Cons: limited readability. 5. **Switch**: Pros: fast, readable; Cons: requires exact type names. 6. **Object-Get**: Pros: similar performance to Object-Function; Cons: less readable. **Library** None of the test cases use external libraries. **Special JS Features/Syntax** No special JavaScript features or syntax are used in these test cases. **Other Considerations** When choosing a condition searching approach, consider factors such as: * Readability: How easy is the code to understand and maintain? * Performance: What's the execution time for each approach? * Browser support: Are all browsers supported equally? **Alternatives** If you need more flexibility or customization options, consider using: 1. **Template literals**: Can be used with Switch statements for more readable type checks. 2. **Type guards**: Can be used with If-Else statements for more robust type checking. 3. **Object destructuring**: Can be used to simplify Object-Plain and Object-Get approaches. Keep in mind that each approach has its strengths and weaknesses, and the best choice depends on your specific use case and performance requirements.
Related benchmarks:
Conditionals (if-else, ternary, switch, object-function, object-plain,object-get)
!! vs. hasOwn vs. in with null obj
if undefined comparison
if undefined comparison with binary
Comments
Confirm delete:
Do you really want to delete benchmark?