Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs String.includes vs Object Hashmap vs Set Large
(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', 'a','b', 'c','d','e','f','g','h','i','j', 'k', 'l','m','n' ,'bar']; var testString = testArray.join(','); var objectHash = testArray.reduce((acc, key) => {acc[key] = true; return acc;},{}); 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 dive into the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of four different string matching approaches: 1. `Array.includes` 2. `String.includes` 3. Object Hashmap 4. ES6 Set Each approach is compared to measure their execution speed on a specific test case. **Test Case Description** The test case creates an array `testArray` containing 12 strings, including the target strings `'foo'`, `'bar'`, and `'baz'`. The test case also creates two additional data structures: * An object hashmap `objectHash` where each key in `testArray` maps to a value (`true`). * A Set `testSet` containing all elements of `testArray`. **Library and Features** 1. **Object Hashmap**: The `reduce()` method is used to create the hashmap, which iterates over the array and assigns a value (`true`) to each key. 2. **ES6 Set**: The `Set` constructor is used to create the set from the array elements. **Approach Comparison** The four approaches differ in their implementation: 1. **Array.includes**: Iterates through the array using a for loop, checking if the target string is present at each index. 2. **String.includes**: A built-in JavaScript method that searches for the presence of the target string within the specified string (in this case, `testString`). 3. **Object Hashmap**: Directly accesses the value associated with the target key in the hashmap using object notation (`objectHash[testStrFirst]`). 4. **ES6 Set**: Uses the `has()` method to check if the target element is present in the set. **Pros and Cons** * **Array.includes**: Simple, but may not be optimized for performance. + Pros: Easy to understand, no dependencies required. + Cons: May have slower execution speed compared to other approaches. * **String.includes**: Built-in method with good optimization, but may not work as expected in certain edge cases. + Pros: Fast and reliable, built-in method. + Cons: May not be optimized for large strings or arrays. * **Object Hashmap**: Direct access to data, potentially fast. + Pros: Fast lookup, direct access. + Cons: Requires manual creation of the hashmap, may have slower initialization time. * **ES6 Set**: Optimized for set operations, fast lookups. + Pros: Fast and efficient, optimized for set operations. + Cons: Requires explicit set creation, may not be suitable for non-set data. **Considerations** When choosing an approach, consider the following: * Performance requirements: If speed is critical, `String.includes` or ES6 Set might be a better choice. For larger datasets, object hashmap might provide faster lookups. * Data structure complexity: If you need to store and manipulate large amounts of data, consider using an object hashmap or ES6 Set for their optimized performance. **Alternatives** Other alternatives to these approaches include: * **Regular expressions**: Can be used to match strings, but may not be as efficient as the above methods. * **Substring search algorithms**: Such as KMP (Knuth-Morris-Pratt) or Rabin-Karp, which can provide faster performance for certain use cases.
Related benchmarks:
Array.includes vs String.includes vs Object Hashmap vs Set vs Array.indexOf Large
equality vs includes
With defined array, Array.includes vs String.includes vs Object Hashmap vs Set vs Array.indexOf Large
equals vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?