Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set value if non existent
(version: 0)
Comparing performance of:
ternary vs if
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
self.attributeHandler = {} self.initialValue = 123
Tests:
ternary
attributeHandler.default = 'default' in attributeHandler ? attributeHandler.default : initialValue
if
if (!('default' in attributeHandler)) attributeHandler.default = initialValue
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ternary
if
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ternary
5733707.0 Ops/sec
if
17097682.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its associated JSON data. **Benchmark Overview** The provided benchmark tests two different ways of setting a default value if it doesn't exist in an object, specifically using JavaScript's ternary operator or an `if` statement with conditional assignment. The test aims to compare the performance difference between these two approaches on modern JavaScript engines. **Test Cases** There are two individual test cases: 1. **Ternary**: This test case uses the ternary operator (`?:`) to set the default value if it doesn't exist in the `attributeHandler` object. ```javascript attributeHandler.default = 'default' in attributeHandler ? attributeHandler.default : initialValue ``` 2. **If**: This test case uses an `if` statement with conditional assignment to set the default value if it doesn't exist in the `attributeHandler` object. ```javascript if (!('default' in attributeHandler)) attributeHandler.default = initialValue ``` **Library and Purpose** Neither of these test cases explicitly uses a library. The code snippets are simple JavaScript expressions, so they don't require any external dependencies. **Special JS Features or Syntax** Both test cases use a relatively modern feature: the ternary operator (`?:`). This operator was introduced in ECMAScript 2015 (ES6) and provides a concise way to express conditional statements. **Pros and Cons of Each Approach** 1. **Ternary Operator**: * Pros: + More concise and readable code. + Expresses the intent clearly, making it easier for others to understand. * Cons: + May be less efficient due to the need to evaluate both conditions (true and false). 2. **If Statement with Conditional Assignment**: * Pros: + Can be more efficient since only one condition needs to be evaluated. * Cons: + Less readable code compared to the ternary operator. + Requires explicit assignment, which can make it harder to understand the intent. **Other Considerations** When comparing these two approaches, consider the following: * Performance: The `if` statement with conditional assignment might be slightly faster due to the reduced number of evaluations. However, this difference is likely to be negligible in most cases. * Readability and Maintainability: The ternary operator is generally more readable and maintainable since it clearly expresses the intent. **Alternative Approaches** Other ways to set a default value if it doesn't exist in an object include: * Using the `||` operator (logical OR): `attributeHandler.default = attributeHandler.default || initialValue` * Using the `in` operator with `??` (optional chaining): `attributeHandler.default ?? initialValue` While these alternatives might be more concise, they might not offer significant performance improvements over the original approaches. In conclusion, the provided benchmark compares two common ways of setting default values in JavaScript using ternary operators and `if` statements. The choice between these approaches depends on a balance between readability, maintainability, and potential performance considerations.
Related benchmarks:
ES6 property (get/set) & getter/setter function & direct access to object attribute
ES6 property get & getter function & direct access to object attribute
setAttribute vs Object.assign
field vs property2
Comments
Confirm delete:
Do you really want to delete benchmark?