Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Boolean vs String comparison for discriminated unions, Obj
(version: 0)
Comparing performance of:
rObjB vs rObjBStrict vs rObjStr vs rObjProp vs rObjPropIn vs rObjPropOpt
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 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', '');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
rObjB
rObjBStrict
rObjStr
rObjProp
rObjPropIn
rObjPropOpt
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 provided benchmark and explain what's being tested, compared, and other considerations. **Benchmark Overview** The benchmark compares four different ways to concatenate strings in JavaScript: 1. Using `reduce()` with implicit coercion (e.g., `v.ok ? acc + 'yes' : acc + 'no'`) 2. Using `reduce()` with explicit coercion (`===` operator) (e.g., `v.ok === true ? acc + 'yes' : acc + 'no'`) 3. Using the optional chaining operator (`?.`) (e.g., `v?.ok ? acc + 'yes' : acc + 'no'`) 4. Checking if a property exists using the `in` operator (e.g., `'ok' in v? acc + 'yes' : acc + 'no'`) **Comparison** The benchmark tests these four approaches on two types of data: 1. Boolean values (`arrB`) and 2. Objects with boolean properties (`arrObjB`, `arrObjStr`, and `arrObjProp`). **Pros and Cons** Here's a brief overview of the pros and cons of each approach: 1. **Implicit Coercion (using `reduce()` with implicit coercion)**: * Pros: Simple, concise, and easy to read. * Cons: Can lead to unexpected behavior if the values being concatenated are not numbers or strings. 2. **Explicit Coercion (`===` operator)**: * Pros: Ensures explicit type checking and avoids potential pitfalls of implicit coercion. * Cons: More verbose and less concise than the implicit approach. 3. **Optional Chaining Operator (`?.`)**: * Pros: Modern, expressive, and safe (only attempts to access properties if they exist). * Cons: Requires support for the optional chaining operator in older browsers or environments. 4. **In-Operator (`in` operator)**: * Pros: Simple, concise, and widely supported (works in most JavaScript engines). * Cons: May not be as expressive or modern as other approaches. **Library Usage** None of the provided benchmarks use any external libraries. The `reduce()` function is a built-in JavaScript method. **Special JS Features or Syntax** The benchmark uses the optional chaining operator (`?.`) and the in-operator (`in`), which are relatively modern features introduced in ECMAScript 2020 (ES2020). Now, regarding alternative approaches: Other alternatives to consider when concatenating strings in JavaScript include: 1. Using a loop instead of `reduce()`. 2. Using template literals. 3. Using `+` operator with string concatenation. However, these alternatives may not offer significant performance benefits over the approaches tested in this benchmark. If you're interested in exploring further optimizations or alternative approaches, I can provide more information on those topics!
Related benchmarks:
Some vs. Filter vs. indexOf vs. includesasd
MapIncludes vs Find on collection
Some vs. Filter vs. indexOf vs. includes vs reduce
Some vs. Filter vs. indexOf vs. Map+Includes vs. Find
Set has vs object key vs map has
Comments
Confirm delete:
Do you really want to delete benchmark?