Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String Union Predicates B
(version: 0)
Comparing performance of:
Array.includes vs Set.has vs switch
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var roles = ['owner', 'writer', 'reader', 'freeBusyReader']; var roleSet = new Set(roles); function isRoleA(str) { return roles.includes(str); } function isRoleS(str) { return roleSet.has(str); } function isRoleSw(str) { switch (str) { case 'owner': case 'writer': case 'reader': case 'freeBusyReader': return true; default: return false; }; } var testees = ['owner', 'writer', 'reader', 'freeBusyReader', 'ower', 'wrer', 'reer', 'freeBueader', null, undefined, 5, 'ownea', 'writea', 'aaaaaa', 'freeBusyReadea', 'ownea', 'writea', 'readea', 'freeBusyReadea']
Tests:
Array.includes
const rA = testees.map(s => isRoleA(s));
Set.has
const rS = testees.map(s => isRoleS(s));
switch
const rSw = testees.map(s => isRoleSw(s));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes
Set.has
switch
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):
**Benchmark Overview** The provided benchmark, "String Union Predicates B", tests the performance of three different approaches to implement union predicates in JavaScript: `Array.includes`, `Set.has`, and `switch` statements. **What is being tested?** In this benchmark, we have a set of test cases that exercise each approach on a dataset of strings. The testees array contains various values, including strings, null, undefined, numbers, and other non-string characters. Each test case uses one of the three approaches to check if a given string is in the set of defined roles (`roleSet`). **Approaches being compared** 1. **Array.includes**: This method checks if an element exists in the array. 2. **Set.has**: This method checks if a value exists in the set. 3. **switch statement**: This statement uses a series of cases to determine which action to take. **Pros and Cons of each approach:** * **Array.includes**: + Pros: Widely supported, easy to implement, and efficient for small datasets. + Cons: Can be slow for large datasets (O(n) complexity), may not be suitable for set operations due to its array-based implementation. * **Set.has**: + Pros: Efficient for large datasets (O(1) complexity), provides a more direct implementation of set membership testing. However, it's a newer feature and might not be supported in older browsers or versions of JavaScript. + Cons: May require additional setup or polyfills if not supported by the environment, and its performance can degrade if the set is very large. * **switch statement**: + Pros: Can provide good performance for small to medium-sized datasets due to its compile-time optimization capabilities. However, it may become less efficient as the number of cases increases. + Cons: Limited scalability, requires explicit case definition, and can lead to slower code if not implemented correctly. **Library usage** In this benchmark, there is no explicit library usage beyond the `Set` class, which is a built-in JavaScript data structure. However, some browsers may have polyfills or additional libraries that can affect performance. **Special JS features or syntax** There are no special JS features or syntax used in this benchmark besides the use of modern JavaScript features like arrow functions (`=>`) and template literals (`\r\n`). **Other alternatives** Alternative approaches for union predicates might include: * Using a `Map` data structure instead of a set. * Implementing a custom lookup table for efficient membership testing. * Utilizing advanced algorithms or data structures, such as Bloom filters or prefix trees. Keep in mind that the choice of approach depends on the specific requirements and constraints of your application. The benchmark is designed to provide insights into the relative performance characteristics of these three approaches under various scenarios.
Related benchmarks:
Lodash "unionWith"
Lodash "unionWith" "unionBy" 4
Lodash "unionWith" "unionBy" and isEqual
Lodash "uniqWith" "uniqBy" "unionWith" "unionBy"
Lodash "unionWith" "unionBy" corrected
Comments
Confirm delete:
Do you really want to delete benchmark?