Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes vs loop
(version: 0)
Comparing performance of:
function vs includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; function search(arr, has) { for (let e of arr) { if (e === has) { return true; } } }
Tests:
function
search(array, "a"); search(array, "c"); search(array, "x"); search(array, "z"); search(array, "f"); search(array, "h"); search(array, "x"); search(array, "i"); search(array, "a"); search(array, "n"); search(array, "r"); search(array, "y");
includes
array.includes("a"); array.includes("c"); array.includes("x"); array.includes("z"); array.includes("f"); array.includes("h"); array.includes("x"); array.includes("i"); array.includes("a"); array.includes("n"); array.includes("r"); array.includes("y");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
function
includes
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark is defined in two parts: `Script Preparation Code` and `Html Preparation Code`. The script preparation code defines a JavaScript function called `search` that takes an array and a string as arguments. The function iterates over the array using a `for...of` loop and checks if the current element matches the search string. **Options being compared** Two options are being compared: 1. **Manual loop**: The `search` function uses a manual loop to iterate over the array. 2. **Array method: includes()**: The second test case uses the built-in `includes()` method of the array, which is likely to be implemented in C++ under the hood and optimized for performance. **Pros and Cons** 1. **Manual Loop**: * Pros: Control over iteration, can be modified easily. * Cons: Can be slow due to the overhead of the loop, may not be optimized by the JavaScript engine. 2. **Array Method: includes()**: * Pros: Optimized for performance, likely to be implemented in C++ and executed quickly. * Cons: Limited control over iteration, may not work as expected if the array is modified. Other considerations: * The benchmark uses a small array with 24 elements, which might not be representative of larger arrays or more complex scenarios. * The `search` function only checks for exact matches, while the `includes()` method also returns true for partial matches. This might affect the results in certain cases. * The benchmark is running on Chrome 113 on a desktop platform, which might not be representative of other browsers or platforms. **Library and Special JS feature** There is no library explicitly mentioned in the benchmark definition. However, the `includes()` method is a standard JavaScript method that uses native code under the hood to perform its operations. **Test users special JS feature** This test does not use any special JavaScript features like async/await, generators, or modern JavaScript syntax like destructuring or arrow functions. Now, let's talk about alternative benchmarking approaches: 1. **Manual loop with Array.prototype.forEach()**: This would replace the `for...of` loop with `Array.prototype.forEach()`, which is another way to iterate over an array in JavaScript. 2. **Manual loop with Array.prototype.indexOf()**: This would use the `Array.prototype.indexOf()` method instead of a manual loop to find the index of the search string in the array. 3. **Parallel execution**: The benchmark could be modified to execute both options simultaneously using parallel processing, which might reveal differences in performance that are not apparent in sequential execution. Keep in mind that each alternative approach has its own pros and cons, and you would need to consider these factors when deciding which one to use for your specific benchmarking needs.
Related benchmarks:
lodash uniq vs array from set
Aviiiiii
lodash uniq vs set/array
lodash uniq vs set into array
lodash uniq vs array to set to array
Comments
Confirm delete:
Do you really want to delete benchmark?