Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs String.includes vs Object Hashmap vs Set vs Array.indexOf (Testing unique 0...N)
(version: 0)
Matching a string against more than one possibility.
Comparing performance of:
Array.includes vs String.includes vs Object Hash-map vs ES6 Set vs Array.indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var format = (i) => new Date('2020-01-01 00:00:00.' + i.toString().padStart(3, '0')).getTime().toString(); var testArray = Array.from(Array(1000).keys()).map(format); var testString = testArray.join(','); var objectHash = testArray.reduce((acc, key) => {acc[key] = true; return acc;},{}); var testSet = new Set(testArray);
Tests:
Array.includes
Array.from(Array(1000).keys()).map(i => testArray.includes(format(i)));
String.includes
Array.from(Array(1000).keys()).map(i => testString.includes(format(i)));
Object Hash-map
Array.from(Array(1000).keys()).map(i => !!objectHash[format(i)]);
ES6 Set
Array.from(Array(1000).keys()).map(i => testSet.has(format(i)));
Array.indexOf
Array.from(Array(1000).keys()).map(i => testArray.indexOf(format(i)) > -1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Array.includes
String.includes
Object Hash-map
ES6 Set
Array.indexOf
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 benchmark and explain what's being tested. **Overview** The benchmark tests the performance of different methods to match a string against an array or a set of unique values. The test cases use various JavaScript features, including `includes`, `has`, and `indexOf`. **Options Compared** Here are the options being compared: 1. **Array.includes**: Tests if a value exists in an array using the `includes` method. 2. **String.includes**: Tests if a substring exists within another string using the `includes` method. 3. **Object Hash-map**: Tests if a key-value pair exists in an object using a hash map implementation (not built-in to JavaScript, but likely implemented by the test). 4. **ES6 Set**: Tests if a value exists in a set data structure using the `has` method. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: 1. **Array.includes**: * Pros: Simple, straightforward implementation. * Cons: May have performance issues for large arrays or when searching for values that are not sequential in the array. 2. **String.includes**: * Pros: Fast and efficient for substring matching. * Cons: May be slower than `Array.includes` for larger datasets due to the overhead of string concatenation and comparison. 3. **Object Hash-map**: * Pros: Can provide fast lookups in O(1) time, especially if implemented efficiently using a hash table. * Cons: Requires extra memory for storing the hash map data structure and may be slower than other approaches due to the overhead of iteration. 4. **ES6 Set**: * Pros: Fast and efficient for set operations using `has`, with an average time complexity of O(1). * Cons: May not be as suitable for searching values in a specific order, unlike arrays. **Other Considerations** Some additional considerations: * The benchmark uses a custom function `format` to generate unique numbers, which may impact the results. * The test data consists of 1000 unique values, which is a relatively small dataset. * The benchmark only tests the performance of each method on this specific dataset and does not account for edge cases or real-world usage scenarios. **Alternative Approaches** If you wanted to rewrite these benchmarks using different approaches, here are some alternatives: 1. **Using `Map` instead of Object Hash-map**: JavaScript's built-in `Map` data structure can provide fast lookups in O(1) time. 2. **Using a custom search algorithm**: Implementing a custom binary search algorithm could potentially outperform the existing methods for large datasets. 3. **Using parallel processing or multithreading**: If available, utilizing multiple CPU cores to perform the searches concurrently could significantly improve performance. Keep in mind that these alternatives would likely require significant changes to the benchmark code and may not be directly comparable to the original results.
Related benchmarks:
Array.includes vs String.includes vs Object Hashmap vs Set Large
Array.includes vs String.includes vs Object Hashmap vs Set vs Array.indexOf Large
Array.includes vs String.includes vs Object Hashmap vs Set vs Array.indexOf (Testing 0...N)
With defined array, Array.includes vs String.includes vs Object Hashmap vs Set vs Array.indexOf Large
Comments
Confirm delete:
Do you really want to delete benchmark?