Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test_every
(version: 0)
Comparing performance of:
test_1 vs test_2
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array_1 = [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]; var array_2 = [{id: 6}, {id: 7}, {id: 8}]; var array_3 = [{id: 9}, {id: 10}, {id: 11}, {id: 12}, {id: 13}]; var ids_to_check = [1, 5, 8, 6, 11, 3];
Tests:
test_1
var joined_array = [ array_1, array_2, array_3, ] var counter = 0; joined_array.every(array => { array.every(object => { if (ids_to_check.includes(object.id)) { counter++; } return counter >= ids_to_check.length; }); return counter >= ids_to_check.length; })
test_2
var joined_array = [ ...array_1, ...array_2, ...array_3, ] var counter = 0; joined_array.every(object => { if (ids_to_check.includes(object.id)) { counter++; } return counter >= ids_to_check.length; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
test_1
test_2
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of two different approaches to checking if specific IDs exist within multiple arrays using JavaScript's `every()` method. **Option 1: (`test_1`)** * **Code:** The code first joins all three arrays (`array_1`, `array_2`, `array_3`) into a single `joined_array`. Then, it uses nested `every()` methods to iterate through each element in the joined array. Inside the nested loops, it checks if the object's `id` property is present in the `ids_to_check` array. If found, a counter is incremented. The outer `every()` method returns `true` only when all elements have been checked and the counter reaches the length of `ids_to_check`, signifying that all target IDs were found. * **Pros:** Potentially more readable due to the clear nesting structure. * **Cons:** Could be slightly less performant than Option 2 because it iterates through all objects in the joined array, even if a target ID is found early on. **Option 2: (`test_2`)** * **Code:** This option directly uses the spread syntax (`...`) to flatten the three arrays into a single `joined_array`. It then iterates through each object in this array using another `every()` method. For each object, it checks if its `id` is present in `ids_to_check`. * **Pros:** Potentially more performant because it avoids nested loops and stops iterating as soon as a target ID is found. * **Cons:** Might be slightly less readable due to the use of spread syntax and potentially fewer clear visual separations between iterations. **Considerations:** * The choice between these two approaches depends on the specific context and size of the arrays involved. For very large datasets, the performance difference might become more noticeable. * You can also experiment with other methods like `filter()` or `find()` to see if they offer better performance for your use case. Let me know if you'd like a deeper dive into any particular aspect or have other questions!
Related benchmarks:
Array range generating
testasdfasdf
sample_benchmark
lodash flatten vs array.flatMap
lodash flatten vs array.flatMap corrected transform
Comments
Confirm delete:
Do you really want to delete benchmark?