Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript a.includes(i) + a.push(i) vs new Set(a) + concat() + ...spread
(version: 0)
new Set() vs conditional push()
Comparing performance of:
!a.includes(x) => a.push(x) vs new Set(a) + spread
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] var b = [2, 4, 6, 11, 12, 13, 14, 15]
Tests:
!a.includes(x) => a.push(x)
b.forEach(i => { if(!a.includes(i)) a.push(i) });
new Set(a) + spread
[...new Set(a.concat(b))]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
!a.includes(x) => a.push(x)
new Set(a) + spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
!a.includes(x) => a.push(x)
1721530.4 Ops/sec
new Set(a) + spread
1358729.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons of those approaches, library usage, special JavaScript features or syntax, and alternatives. **Benchmark Overview** The benchmark compares two approaches to perform set operations on arrays: 1. **Conditional Push**: The `!a.includes(x)` expression checks if an element is not in the array, and then pushes it using `a.push(i)`. 2. **New Set with Spread**: The `new Set(a.concat(b))` approach creates a new set by concatenating the arrays `a` and `b` using the spread operator (`...`) and then converting the resulting array to a set. **Options Compared** The benchmark compares these two approaches: * Conditional Push: `!a.includes(x) => a.push(x)` * New Set with Spread: `new Set(a) + concat() + ...spread` **Pros and Cons of Each Approach** **Conditional Push** Pros: * More JavaScript-like syntax, which might be easier to read and write for some developers. * Can be more efficient in certain scenarios, as it avoids the overhead of creating a new set. Cons: * May not be as concise or expressive as using `new Set()` directly. * Can lead to slower performance compared to the `new Set()` approach due to the overhead of iterating over the array and pushing elements. **New Set with Spread** Pros: * More concise and expressive syntax, making it easier to understand and write. * Generally faster than conditional push, as it avoids iteration and pushes elements directly onto the set. Cons: * May require more JavaScript knowledge to use correctly. * Can lead to slower performance in certain scenarios due to the overhead of creating a new set and concatenating arrays. **Library Usage** There is no explicit library usage mentioned in this benchmark. However, some libraries might be implicitly used due to browser-specific features or polyfills (e.g., `console.log()`). **Special JavaScript Features or Syntax** The benchmark uses the following special JavaScript feature: * The spread operator (`...`) is not explicitly defined as a special feature, but it's used in the `new Set(a.concat(b))` approach. However, it's worth noting that the spread operator was introduced in ECMAScript 2015 (ES6) and has since become a standard part of JavaScript. **Alternatives** Other alternatives to these approaches could include: * Using an existing set data structure, such as `Set()` or `MSet`, which might be faster but also more complex. * Implementing a custom set-like data structure using arrays or other data structures, which might be more efficient but also more complex and less readable. * Using a library like Lodash or Ramda to perform set operations, which might provide additional features and performance benefits. Overall, the benchmark provides a useful comparison of two common approaches to performing set operations in JavaScript. By understanding the pros and cons of each approach, developers can make informed decisions about how to optimize their code for specific use cases.
Related benchmarks:
test spread vs concat
slice vs. spread operator
Spread operator vs Array.prototype.apply vs Array.prototype.concat
concat vs spread three arrays
Comments
Confirm delete:
Do you really want to delete benchmark?