Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object vs set.has vs. array.includes on strings
(version: 0)
Comparing performance of:
includes vs lookup vs object
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ["x", "y", "width", "height", "showTrend", "showLabel", "somePropertyWithExtremelyLongName", "originX", "originY", "name"]; var b = new Set(a); var c = { "x": true, "y": true, "width": true, "height": true, "showTrend": true, "showLabel": true, "somePropertyWithExtremelyLongName": true, "originX": true, "originY": true, "name": true }
Tests:
includes
return a.includes("somePropertyWithExtremelyLongName")
lookup
return b.has("somePropertyWithExtremelyLongName")
object
return c["somePropertyWithExtremelyLongName"]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
object
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 JSON and explain what's being tested. The test is comparing three different approaches to check if a specific string exists in an array, object, or set: 1. **Array's `includes()` method**: This method checks if a given value (in this case, "somePropertyWithExtremelyLongName") is present in the array. 2. **Set's `has()` method**: This method checks if a given value (in this case, "somePropertyWithExtremelyLongName") is present in the set. 3. **Object's key lookup using square brackets (`[]`)**: This approach directly accesses an object property by its name. Now, let's discuss the pros and cons of each approach: **Array's `includes()` method** Pros: * Easy to read and write * Works with arrays of any type (not just strings) * Returns a boolean value indicating presence or absence Cons: * Can be slower for large arrays due to iteration * May not perform well if the array contains many duplicate values **Set's `has()` method** Pros: * Fast lookups, even for large sets * Efficient use of memory, as sets automatically eliminate duplicates Cons: * Limited to checking membership in a set; can't access other properties * Requires setting up the set beforehand (in this case, using `new Set(a)`) **Object's key lookup using square brackets (`[]`)** Pros: * Direct and efficient access to object properties by name * Works with objects of any type (not just strings) Cons: * Can be slower than using a set for very large numbers of keys * Requires knowing the exact property names beforehand In this specific test, the results show that the `object` approach is the fastest, followed closely by the `includes()` method. The `lookup` (set's `has()` method) is slightly slower. As for libraries and special JS features: * No external library is used in this test. * There are no special JS features or syntax mentioned; all standard JavaScript concepts are employed. Other alternatives to consider: * For large arrays, using a `Map` data structure might be faster than an array's `includes()` method. * For very large sets, using a `WeakSet` instead of a regular set might be more efficient if you're dealing with objects that could be garbage collected. * For direct property access in objects, using a `Map` data structure or a custom data structure (like a trie) might provide better performance than a simple object lookup. Keep in mind that these alternatives will depend on the specific requirements and constraints of your use case.
Related benchmarks:
Includes (array) vs Has (Set)
set.has vs. array.includes (string input)
Small n set vs array
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?