Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Boolean vs String comparison for discriminated unions, Obj, StrOrEmpty
(version: 0)
Comparing performance of:
rObjB vs rObjBStrict vs rObjStr vs rObjProp vs rObjPropIn vs rObjPropOpt vs rObjStrOrEmpty
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getRandoBoolean() { return !!(Math.floor(Math.random() * 2)); } function getRandoString() { return !!(Math.floor(Math.random() * 2)) ? 'accepted' : 'rejected'; } function getRandoShortString() { return !!(Math.floor(Math.random() * 2)) ? 'y' : ''; } var arrB = [...(new Array(1000))].map(v => getRandoBoolean()); var arrStr = [...(new Array(1000))].map(v => getRandoString()); var arrShortStr = [...(new Array(1000))].map(v => getRandoShortString()); var arrObjStrOrEmpty = [...(new Array(1000))].map(v => getRandoBoolean() ? {ok : 'ok' ,b: 'value'}: {ok : '' ,c: 'value'}); var arrObjB = [...(new Array(1000))].map(v => getRandoBoolean() ? {ok : true ,b: 'value'}: {ok : false ,c: 'value'}); var arrObjStr = [...(new Array(1000))].map(v => getRandoBoolean() ? {ok : 'yes' ,b: 'value'}: {ok : 'no' ,c: 'value'}); var arrObjProp = [...(new Array(1000))].map(v => getRandoBoolean() ? {ok : true ,b: 'value'}: { c: 'value'});
Tests:
rObjB
const rObjB = arrObjB.reduce((acc, v) => v.ok ? acc + 'yes' : acc + 'no', '');
rObjBStrict
const rObjBStrict = arrObjB.reduce((acc, v) => v.ok === true ? acc + 'yes' : acc + 'no', '');
rObjStr
const rObjStr = arrObjStr.reduce((acc, v) => v.ok === 'yes' ? acc + 'yes' : acc + 'no', '');
rObjProp
const rObjProp = arrObjProp.reduce((acc, v) => v.ok ? acc + 'yes' : acc + 'no', '');
rObjPropIn
const rObjPropIn = arrObjProp.reduce((acc, v) => 'ok' in v? acc + 'yes' : acc + 'no', '');
rObjPropOpt
const rObjPropOpt = arrObjProp.reduce((acc, v) => v?.ok ? acc + 'yes' : acc + 'no', '');
rObjStrOrEmpty
const rObjStrOrEmpty = arrObjStrOrEmpty.reduce((acc, v) => v.ok ? acc + 'yes' : acc + 'no', '');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
rObjB
rObjBStrict
rObjStr
rObjProp
rObjPropIn
rObjPropOpt
rObjStrOrEmpty
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):
Measuring performance differences in JavaScript is a complex task, and MeasureThat.net provides a valuable resource for developers to compare various approaches. **Benchmark Overview** The provided JSON represents a benchmark with multiple test cases, each testing the speed of string concatenation using different approaches. The test cases are: 1. `rObjBStrict`: Tests the use of strict equality (`===`) for string concatenation. 2. `rObjPropIn`: Tests the use of object property existence check (`'ok' in v`). 3. `rObjPropOpt`: Tests the use of optional chaining (`?.ok`) for string concatenation. 4. `rObjStr`: Tests the use of simple equality (`==`) for string concatenation. 5. `rObjB`: Tests the use of loose equality (`==`) for string concatenation, which is generally slower than strict equality. **Options Compared** The benchmark compares the following options: 1. **Strict Equality (`===`)**: This approach uses strict equality to check if a property exists in an object. It ensures that `undefined`, `null`, and `true` values are treated as distinct from other values. 2. **Loose Equality (`==`)**: This approach uses loose equality to check if a property exists in an object. However, it can lead to unexpected behavior with certain data types. 3. **Object Property Existence Check (`'key' in obj`)**: This approach checks if a specific key exists in an object using the `in` operator. It provides more flexibility than strict equality but may be slower due to the overhead of the check. 4. **Optional Chaining (`?.property`)**: This approach uses optional chaining to safely access nested properties without causing errors when a property is missing. **Performance Results** The benchmark results show that: 1. `rObjBStrict`: Uses strict equality, which yields the fastest execution times. 2. `rObjPropIn`: Uses object property existence check, which provides more flexibility but slower performance compared to strict equality. 3. `rObjPropOpt`: Uses optional chaining, which balances safety with performance by avoiding unnecessary checks. 4. `rObjStr`: Uses loose equality, which is generally the slowest due to its less stringent behavior. **Conclusion** When it comes to string concatenation in JavaScript, using strict equality (`===`) provides the fastest execution times. While object property existence check (`'key' in obj`) and optional chaining (`?.property`) offer more flexibility, they may come at a performance cost. Looseness of comparison (`==`) should be avoided due to its potential for unexpected behavior. In general, developers should strive to use strict equality and optional chaining when working with object properties to balance safety and performance. If you're interested in optimizing string concatenation, I recommend exploring alternative approaches using `Array.prototype.push()` or `String.prototype +=`, which can provide better performance than the current benchmark results.
Related benchmarks:
Some vs. Filter vs. indexOf vs. includesasd
MapIncludes vs Find on collection
Some vs. Filter vs. indexOf v includes
Some vs. Filter vs. indexOf vs. includes vs reduce
Some vs. Filter vs. indexOf vs. Map+Includes vs. Find
Comments
Confirm delete:
Do you really want to delete benchmark?