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",
"*",
"#"
]
var tempResult = inputs.map(input=>keys.find(key=>key.number===input))
var tempResult = inputs.map(input=>keys.map(key=>key.number).includes(input))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Find | |
Includes |
Test name | Executions per second |
---|---|
Find | 2580886.2 Ops/sec |
Includes | 739356.9 Ops/sec |
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:
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.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
find()
:find()
only returns the first matching element, while includes()
can search through the entire array.includes()
: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:
forEach()
: Instead of find()
or includes()
, you could use the forEach()
method to iterate over the array and check for matches.includes()
approach works.Here's an example of how the benchmark could be implemented using a loop:
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()
.