Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Foreach vs Array.includes
(version: 0)
Comparing performance of:
Array.includes vs For
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
Tests:
Array.includes
var result = array.includes('d');
For
for (let i = 0; i < array.length; i++) { if (array[i] === 'd') { var result = true; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes
For
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 what's being tested in this benchmark. **Benchmark Definition JSON** The provided JSON represents the overall benchmark, which is named "Foreach vs Array.includes". The description field is empty, and there are two script preparation codes: * `var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];` is used for both test cases. * No HTML preparation code is provided. **Individual Test Cases** There are two individual test cases: 1. **Array.includes** The benchmark definition for this test case is a simple `includes()` method call on the defined array, searching for the string `'d'`. ```javascript var result = array.includes('d'); ``` 2. **For** The benchmark definition for this test case uses a traditional `for` loop to iterate over the array and check if the current element is equal to `'d'`. The loop breaks as soon as it finds the match. ```javascript for (let i = 0; i < array.length; i++) { if (array[i] === 'd') { var result = true; break; } } ``` **Options Compared** The two test cases compare the performance of: * The `includes()` method * A traditional `for` loop with a conditional statement **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Array.includes()** + Pros: - Concise and readable syntax - Built-in implementation, so no overhead for initialization + Cons: - May have performance implications due to regular expression engine usage (if not directly implemented by the browser) - Might be slower than a traditional `for` loop due to method call overhead * **Traditional For Loop** + Pros: - Direct, low-level control over iteration - No method call overhead or potential performance implications from regular expression engine usage + Cons: - Less readable and maintainable syntax - Requires manual index management **Library** The `includes()` method uses the ECMAScript standard's built-in implementation of the `Array.prototype.includes()` method, which is typically implemented using a binary search algorithm. The browser implements this method efficiently. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in these benchmark definitions. **Alternatives** If you wanted to create an alternative benchmark, you could consider: * Using different array sizes or data distributions (e.g., random numbers instead of consecutive letters) * Adding additional operations within the loop or `includes()` method call * Comparing other iteration techniques, such as `forEach()`, `map()`, or `reduce()` * Using a different browser or platform to test compatibility and performance differences These alternatives would allow you to explore various aspects of array manipulation and iteration in JavaScript.
Related benchmarks:
Array.includes() vs Array.indexOf()
Array.includes vs OR operator
Includes (array) vs Some (array)
equals vs includes
#2 Array Includes vs. Find
Comments
Confirm delete:
Do you really want to delete benchmark?