Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
testing ternary vs &&
(version: 0)
Comparing performance of:
spread ternary vs spread &&
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function translateBySpread (a) { const b = { ...(a.id && { id: a.id }), ...(a.foo && { foo: a.foo}) } return b; } function translateByTernary (a) { const b = { ...(a.id ? { id: a.id } : {}), ...(a.foo ? { foo: a.foo} : {}) } return b; }
Tests:
spread ternary
translateByTernary({ id: '12345', type: 'article' });
spread &&
translateBySpread({ id: '12345', type: 'article' });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread ternary
spread &&
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15
Browser/OS:
Safari 17 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread ternary
910627.1 Ops/sec
spread &&
1195137.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Definition** The benchmark defines two functions: `translateBySpread` and `translateByTernary`. Both functions create an object `b` with properties `id` and `foo`, depending on whether their corresponding values are truthy in `a`. * `translateBySpread` uses the spread operator (`...`) to merge properties from `a.id && { id: a.id }` and `a.foo && { foo: a.foo }`. This approach is called "spread operator syntax" or "rest parameter syntax." * `translateByTernary` uses a ternary operator (`? :`) to check if the value of `a.id` or `a.foo` is truthy, and only include the property in object `b` if it is. This approach is called "ternary operator syntax" or "conditional expression." **Comparison** The benchmark compares the performance of these two approaches: * Spread operator syntax (`translateBySpread`) * Ternary operator syntax (`translateByTernary`) **Pros and Cons:** * **Spread Operator Syntax (`translateBySpread`)** * Pros: + Can be more concise and readable for simple object creation + Works well with modern JavaScript features like rest parameters and destructuring * Cons: + May have slower performance due to the creation of intermediate objects * **Ternary Operator Syntax (`translateByTernary`)** * Pros: + Can be more efficient in terms of execution time, as it avoids creating intermediate objects + Works well for simple object creation and filtering * Cons: + May require more lines of code to achieve the same result **Library Usage** None of the provided functions use any external libraries. **Special JavaScript Features or Syntax** The benchmark uses modern JavaScript features like: * Spread operator (`...`) * Rest parameters (in `translateBySpread`) These features are widely supported in modern browsers and are used extensively in modern web development.
Related benchmarks:
const vs direct usages
&& vs ternary
ternary vs !!
Ternary vs Condition v2
let vs const vs var
Comments
Confirm delete:
Do you really want to delete benchmark?