Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs String.includes vs Object Hashmap vs Set
(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
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArray = ['foo', 'bar']; var testString = "foo,bar"; var objectHash = { foo: true, bar: true, }; var testSet = new Set(testArray);
Tests:
Array.includes
const testStrFirst = 'foo'; testArray.includes(testStrFirst); const testStrSecond = 'bar'; testArray.includes(testStrSecond); const testStrNotMatch = 'baz'; testArray.includes(testStrNotMatch);
String.includes
const testStrFirst = 'foo'; testString.includes(testStrFirst); const testStrSecond = 'bar'; testString.includes(testStrSecond); const testStrNotMatch = 'baz'; testString.includes(testStrNotMatch);
Object Hash-map
const testStrFirst = 'foo'; objectHash[testStrFirst]; const testStrSecond = 'bar'; objectHash[testStrSecond]; const testStrNotMatch = 'baz'; objectHash[testStrNotMatch];
ES6 Set
const testStrFirst = 'foo'; testSet.has(testStrFirst); const testStrSecond = 'bar'; testSet.has(testStrSecond); const testStrNotMatch = 'baz'; testSet.has(testStrNotMatch);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.includes
String.includes
Object Hash-map
ES6 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 benchmark and its various components. **Benchmark Overview** The benchmark is designed to compare the performance of different methods for matching a string against multiple possibilities: 1. `Array.includes` 2. `String.includes` 3. Using an object hash map (`objectHash`) 4. Using an ES6 Set (`testSet`) **Options Compared** Each option has its pros and cons: ### Array.includes * Pros: + Widely supported and well-maintained. + Fast and efficient for matching against a large array of values. * Cons: + Can be slower than other options for small arrays or when using modern browsers with optimized string lookup algorithms. ### String.includes * Pros: + Specifically designed for string matching, making it more efficient than Array.includes for larger strings. + Fast and reliable for modern browsers. * Cons: + Less flexible than Array.includes, as it only works with strings. ### Object Hash Map (`objectHash`) * Pros: + Fast and efficient for lookup operations in an object. + Can be used with any type of data (not just strings). * Cons: + Requires manual configuration and management of the hash map. + May not perform as well as Array.includes or String.includes for large datasets. ### ES6 Set (`testSet`) * Pros: + Efficient and fast lookup operations, similar to an object hash map. + Automatically managed and updated by the Set data structure. * Cons: + Requires modern browsers that support the ES6 Set API. + May not perform as well as Array.includes or String.includes for very large datasets. **Library Usage** The benchmark uses the following libraries: 1. `objectHash`: A simple object hash map implementation, likely created by MeasureThat.net to provide a basic comparison point. 2. `testSet`: The native ES6 Set API, used in modern browsers to represent an unordered collection of unique values. **Special JS Feature/Syntax** None mentioned in the benchmark definition or test cases. **Other Considerations** When choosing an approach for string matching, consider the following factors: 1. **Data size**: Larger datasets may benefit from more efficient algorithms like Array.includes or String.includes. 2. **Browser support**: If you need to support older browsers, choose a method that works across multiple versions (e.g., Array.includes). 3. **Customization and control**: Object hash maps and ES6 Sets offer more flexibility for customizing and managing the matching process. **Alternatives** If you don't have access to the MeasureThat.net benchmark or want to explore other options, consider using: 1. `String.prototype.indexOf()`: Similar to String.includes, but returns the index of the first match instead of a boolean result. 2. `Array.prototype.findIndex()`: Similar to Array.includes, but returns the index of the first match instead of a boolean result. Keep in mind that these alternatives may not offer the same performance as the built-in methods used in the benchmark.
Related benchmarks:
equality vs includes
chain of or equals vs includes but smaller
=== vs includes
equals vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?