Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Includes vs forEach
(version: 0)
Comparing performance of:
includes vs forEach
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = Array.from(Array(100).keys(), x => ({ value: x, disabled: false })); var arr2 = Array.from(Array(50).keys(), x => x * 2);
Tests:
includes
arr1.map(x => { if (arr2.includes(x.value)) x.disabled = true; return x; });
forEach
arr1.map(x => { arr2.forEach(y => { if (x.value === y) x.disabled = true; }); return x; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
467426.8 Ops/sec
forEach
277657.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined in JSON format, which includes the following information: * `Name`: The name of the benchmark, which is "Includes vs forEach". * `Description`: An empty string, indicating that there is no description for this benchmark. * `Script Preparation Code`: A JavaScript code snippet that creates two arrays, `arr1` and `arr2`, using the `Array.from()` method with a callback function. The purpose of these arrays is not explicitly stated, but based on their contents, it appears that they are being used to test the performance of different array methods. * `Html Preparation Code`: An empty string, indicating that there is no HTML preparation code. **Individual Test Cases** The benchmark includes two individual test cases: 1. `"includes"`: * `Benchmark Definition`: A JavaScript code snippet that maps over the `arr1` array using a function that checks if each element's value exists in the `arr2` array using the `includes()` method. * Purpose: To test the performance of the `includes()` method on large arrays. 2. `"forEach"`: * `Benchmark Definition`: A JavaScript code snippet that maps over the `arr1` array using a function that iterates over the `arr2` array using the `forEach()` method and checks if each element matches an element in the `arr1` array. **Options Being Compared** The benchmark is comparing the performance of two options: * Using the `includes()` method to check for existence in an array. * Using the `forEach()` method to iterate over an array and perform a matching operation. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: 1. `includes()`: * Pros: + Fast and efficient, as it uses a linear search algorithm that stops as soon as the element is found. + Can be used to test for existence in an array without having to iterate over the entire array. * Cons: + May be slower than other approaches for very large arrays, as the linear search algorithm can be slow. + Requires a single loop iteration to find the match. 2. `forEach()`: * Pros: + Can be used to test multiple operations on an array, such as filtering and mapping. + Does not require a single loop iteration to find matches. * Cons: + May be slower than other approaches for large arrays, as it involves iterating over the entire array. **Library** There is no library being used in this benchmark. The `Array.from()` method is a built-in JavaScript method that creates a new array from an iterable or array-like object. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being used in this benchmark. However, it's worth noting that the use of arrow functions (`=>`) and template literals (`\r\n`) is a modern JavaScript feature that allows for concise and expressive code. **Other Alternatives** If you want to test other approaches to array iteration and lookup, here are some alternatives: * Using `indexOf()` instead of `includes()`. * Using a loop-based approach with an index variable. * Using the `filter()` method to create a new array with matching elements. * Using the `some()` method to check if any element in the array matches a condition. Keep in mind that each alternative may have its own pros and cons, depending on the specific use case and performance requirements.
Related benchmarks:
forEach(item) vs forEach(item, index, array)
map vs forEach Chris v2b
Foreach&Push vs Map2
Array.forEach vs Object.keys().forEach
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Comments
Confirm delete:
Do you really want to delete benchmark?