Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array concat and search
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
concat vs plain vs set
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var a = Array(1000).fill(Math.floor(Math.random() * 9)); var b = Array(1000).fill(Math.floor(Math.random() * 9)); var c = Array(1000).fill(Math.floor(Math.random() * 9)); var d = Array(1000).fill(Math.floor(Math.random() * 9)); var username = 20
Tests:
concat
a.concat(b,c,d).includes(username)
plain
(a.includes(username) == false && b.includes(username) == false && c.includes(username) == false && d.includes(username) == false)
set
new Set([...a, ...b, ...c, ...d]).has(username)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
concat
plain
set
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):
Let's break down the provided JSON and explain what is tested in each benchmark. **Benchmark Definition** The `Script Preparation Code` section generates four arrays, `a`, `b`, `c`, and `d`, each filled with random numbers between 0 and 8. The `Html Preparation Code` includes a reference to the Lodash library, which provides utility functions for working with arrays. **Individual Test Cases** The three test cases are designed to compare different approaches for searching if an array contains a specific value, `username`. Here's what each test case is testing: 1. **"concat"`: This test case uses the `concat` method to concatenate the four arrays into one and then checks if the concatenated array includes the `username`. 2. **"plain"`: This test case uses a simple approach by checking each individual array for the presence of `username`. If any of the arrays don't contain it, the test returns true. 3. **"set"`: This test case creates a Set object from the concatenated arrays using the spread operator (`...`) and then checks if the Set includes the `username`. **Options Compared** The three test cases compare different approaches to search for an element in an array: * **Concatenation**: The `concat` method is used to combine multiple arrays into one. This approach can be inefficient if the concatenated array becomes very large. * **Plain iteration**: The "plain" test case uses individual iterations over each array, which can be slower due to the overhead of checking each element individually. * **Set**: The "set" test case creates a Set object from the concatenated arrays, which allows for efficient lookups using the `has` method. However, creating a Set object may have additional overhead compared to concatenating arrays. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Concatenation**: Pros - simple to implement, can be fast for small arrays. Cons - inefficient for large concatenated arrays. * **Plain iteration**: Pros - simple to implement, suitable for small arrays. Cons - slow due to individual iterations over each array. * **Set**: Pros - efficient lookups, suitable for large datasets. Cons - creates an additional object with overhead. **Library and Special JS Features** The Lodash library is used in the `Html Preparation Code` section. Lodash provides utility functions for working with arrays, including the `concat` method used in the "concat" test case. There are no special JS features mentioned in the provided code. **Alternatives** Other alternatives to compare when searching for an element in an array include: * Using a `for...in` loop or `forEach` method to iterate over each array. * Using a `reduce` method to sum up elements from multiple arrays. * Using a `filter` method to create a new array with only the desired elements. These alternatives may have different performance characteristics and trade-offs compared to the approaches tested in this benchmark.
Related benchmarks:
array find vs _.find more items
array find vs some fork Gabriel
array find vs some vs for of
array includes vs some NO FUCKING SEARCH BOX
Comments
Confirm delete:
Do you really want to delete benchmark?