Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Enum: includes vs find vs indexOf
(version: 0)
Comparing performance of:
includes vs find vs indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var ExecutionState; (function(ExecutionState) { ExecutionState["Failed"] = "FAILED"; ExecutionState["Succeed"] = "SUCCEEDED"; ExecutionState["InProgress"] = "INPROGRESS"; })(ExecutionState || (ExecutionState = {}));
Tests:
includes
const hasThing = Object.values(ExecutionState).includes('SUCCEEDED');
find
const hasThing = Object.values(ExecutionState).find((e) => {e === 'SUCCEEDED'});
indexOf
const hasThing = Object.values(ExecutionState).indexOf('SUCCEEDED');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
find
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 provided benchmark and explain what is tested, the options compared, pros and cons of each approach, library usage, special JS features or syntax (if any), and other alternatives. **Benchmark Overview** The benchmark measures the performance of three different methods for checking if a string value is equal to "SUCCEEDED" in an ExecutionState object: 1. `includes` 2. `find` 3. `indexOf` These methods are used to verify that the execution state has been successfully completed ("Succeeded"). **Method Comparison** The benchmark compares the performance of these three methods, which can be summarized as follows: * `includes`: This method checks if a string value is included in an array using the `includes()` method. It returns true if the value exists in the array. * `find`: This method searches for the first occurrence of a specified value (in this case, "Succeeded") in an array using the `find()` method. It returns the found element or undefined if not found. * `indexOf`: This method finds the index of the first occurrence of a specified value (in this case, "Succeeded") in an array using the `indexOf()` method. If the value is not found, it returns -1. **Pros and Cons** Here are some pros and cons for each approach: * `includes`: + Pros: Simple, efficient, and widely supported. + Cons: May return false positives if the string is present in the array due to locale-specific formatting. * `find`: + Pros: Can be useful when searching for a specific value within an array. + Cons: May not perform well if the array is large or the search value is not found. * `indexOf`: + Pros: Fast and efficient, but may return -1 if the value is not found. + Cons: Returns an index, which might be unnecessary for some use cases. **Library Usage** There is no external library used in this benchmark. The ExecutionState object is a custom data structure created by the user to represent different execution states. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark beyond the standard language features of JavaScript. **Other Alternatives** In addition to these three methods, other alternatives could include: * Using `Array.prototype.every()` method, which checks if all elements in an array pass a test (in this case, equality with "Succeeded"). * Using `Array.prototype.reduce()` method, which applies a reduction function to an array (in this case, checking for the presence of "Succeeded"). However, these alternatives might not be as efficient or straightforward as the original methods used in the benchmark. Keep in mind that this is a microbenchmark, and performance differences between these methods may be small. The goal of a microbenchmark like this one is to measure the performance difference between specific code paths rather than optimizing for maximum performance.
Related benchmarks:
Array find with indexOf vs includes
IndexOf vs Includes on string
array indexOf vs includes vs some v3
find vs includes vs indexof
#2 Array Includes vs. Find
Comments
Confirm delete:
Do you really want to delete benchmark?