Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Check all array values are equal new Set vs every
(version: 0)
Check all array values are equal new Set vs every
Comparing performance of:
new Set vs every()
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [ 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!', 'Hello world!' ]
Tests:
new Set
[...new Set(arr)]
every()
arr.every( (val, i, arr) => val === arr[0] )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Set
every()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.127 Safari/537.36 OPR/60.3.3004.55692
Browser/OS:
Opera 60 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Set
217258.3 Ops/sec
every()
1876389.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. The main goal of this benchmark is to compare two approaches for checking if all elements in an array are equal: 1. **New Set**: This approach uses the `Set` constructor to create a set from the input array. Since sets only contain unique values, if all elements in the original array are equal, the resulting set will have only one element. The benchmark checks how long it takes for this process to complete. 2. **every() method**: This approach uses the `every()` method, which returns `true` if all elements in an array pass the provided test (in this case, checking if each element is equal to the first element of the array). If at least one element fails the test, `every()` returns `false`. The benchmark measures how long it takes for this process to complete. **Comparison of approaches** Both methods have their pros and cons: * **New Set approach**: + Pros: This method is likely to be faster because creating a set only requires iterating over the array once. + Cons: Creating an empty set is an expensive operation, as it involves allocating memory for a new object and potentially triggering some internal cache invalidation. However, this cost is likely to be negligible compared to other operations in the benchmark. * **every() method**: + Pros: This method can be more efficient if there are many duplicate elements in the array, since the test only needs to check each element once. + Cons: This approach requires iterating over the entire array and comparing each element to the first one. While still relatively fast, it may not be as efficient as creating a set. **Library used** In both test cases, no specific library is being tested or used beyond the standard JavaScript API (e.g., `Set`, `every()`). However, if we were to consider third-party libraries for this benchmark, some alternatives could be: * Lodash's `isEqual` function: This would provide a more comprehensive set of comparison functions and might offer better performance. * Ramda's `eq` function: This is another popular functional programming library that provides a range of equality tests. **Special JS features or syntax** There are no special JavaScript features or syntax used in this benchmark. Both approaches rely on standard JavaScript constructs (sets, arrays, and the `every()` method). In summary, this benchmark compares two approaches for checking if all elements in an array are equal: creating a new set versus using the `every()` method. The New Set approach is likely to be faster due to its lower overhead, while the every() method can be more efficient with many duplicate elements.
Related benchmarks:
Array.from vs Array spread with mapping of values2
New Array Object vs New Array Literal
arr.slice() vs [...arr]
array = [] vs array.length = 0
Comments
Confirm delete:
Do you really want to delete benchmark?