Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array find vs includes (temp3214)
(version: 0)
Comparing performance of:
Find vs Includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var keys = [ { number: "1", letters: "" }, { number: "2", letters: "ABC" }, { number: "3", letters: "DEF" }, { number: "4", letters: "GHI" }, { number: "5", letters: "JKL" }, { number: "6", letters: "MNO" }, { number: "7", letters: "PQRS" }, { number: "8", letters: "TUV" }, { number: "9", letters: "WXYZ" }, { number: "*", letters: "" }, { number: "0", letters: "+" }, { number: "#", letters: "" }, ]; var inputs = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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", "*", "#" ]
Tests:
Find
var tempResult = inputs.map(input=>keys.find(key=>key.number===input))
Includes
var tempResult = inputs.map(input=>keys.map(key=>key.number).includes(input))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Find
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 YaBrowser/24.12.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Find
2580886.2 Ops/sec
Includes
739356.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the details of this JavaScript benchmark. **Benchmark Overview** The `Array find vs includes (temp3214)` benchmark compares the performance of two approaches to achieve a similar result: finding an element in an array using either `find()` or `includes()`. The test is designed to measure which approach is faster, with more executions per second. **Options Compared** Two options are compared: 1. **`keys.find(key => key.number === input)`**: This approach uses the `find()` method to search for an element in the `keys` array that matches a given condition (`key.number === input`). The `find()` method returns the first element from the array that satisfies the provided callback function. 2. **`keys.map(key => key.number).includes(input)`**: This approach maps each element of the `keys` array to its `number` property and then uses the `includes()` method to search for a given value (`input`) in the resulting array. **Pros and Cons** 1. **`find()`**: * Pros: More concise and readable, as it directly searches for an element without mapping. * Cons: May be slower if the array is very large, since `find()` only returns the first matching element, while `includes()` can search through the entire array. 2. **`includes()`**: * Pros: Can search through the entire array, making it potentially faster for large datasets. * Cons: Requires more code and may be less readable due to the additional step of mapping. **Library Used** None in this benchmark. Both `find()` and `includes()` are built-in JavaScript methods that do not rely on external libraries. **Special JS Feature/Syntax** This benchmark does not explicitly use any special JavaScript features or syntax, such as async/await, promises, or modern ES6+ features like arrow functions. The code is written in a concise and readable style using standard JavaScript constructs. **Other Alternatives** If you were to write this benchmark from scratch, alternative approaches could include: 1. **Using `forEach()`**: Instead of `find()` or `includes()`, you could use the `forEach()` method to iterate over the array and check for matches. 2. **Using a loop**: A simple loop can be used to compare each element in the array with the input value, similar to how the `includes()` approach works. Here's an example of how the benchmark could be implemented using a loop: ```javascript for (let i = 0; i < keys.length; i++) { if (keys[i].number === input) { // Perform some action when match is found } } ``` Keep in mind that this approach would likely be slower than using `find()` or `includes()`, since it involves more iterations over the array. In summary, this benchmark provides a simple and concise way to compare the performance of two approaches for searching an element in an array: using `find()` versus using `includes()`.
Related benchmarks:
Spread vs object assign
object to array with lodash
Array.from vs Array spread with mapping of values2
Array.find vs map to object then lookup.
Comments
Confirm delete:
Do you really want to delete benchmark?