a = [];
for(var i=0;i<100;i++){
a.push({index:i,data:"blabla"});
}
var indexToFind = 50;
ind = -1;
for(var i =0;i<a.length;i++){
if(a[i].index == indexToFind){
ind = i;
}
}
a[ind].data;
a.find(x => x.index == indexToFind).data;
a.find(function(cv,i){ return cv.index == indexToFind;}).data;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for loop | |
javascript array.find | |
javascript array.find with function |
Test name | Executions per second |
---|---|
for loop | 3717.8 Ops/sec |
javascript array.find | 31035.2 Ops/sec |
javascript array.find with function | 32760.7 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark definition is a JSON object that contains information about the test case. In this case, it's an array of objects with different properties:
Name
: The name of the benchmark.Description
: An optional description of the benchmark (empty in this case).Script Preparation Code
: A JavaScript code snippet that prepares the data for the benchmark.Html Preparation Code
: An optional HTML code snippet, but it's empty in this case.Individual Test Cases
The individual test cases are also JSON objects with different properties:
Benchmark Definition
: The actual JavaScript code that defines the benchmark. This code is executed to measure the performance of the implementation.Test Name
: A brief name for each test case.In this specific example, there are three test cases:
What's being tested
These test cases are designed to measure the performance of different ways to search an array in JavaScript.
Array.prototype.find()
method.Options compared
The three test cases compare different approaches to search an array:
Array.prototype.find()
methodPros and Cons of each approach
Here's a brief summary of the pros and cons of each approach:
Array.prototype.find()
methodLibrary usage
There is no explicit library usage in these test cases. However, the built-in Array.prototype.find()
method uses various internal libraries and algorithms to achieve its performance.
Special JS feature or syntax
The use of arrow functions (=>
) in the third test case is a relatively modern JavaScript feature that provides concise and expressive code.
Other alternatives
If you're interested in exploring alternative approaches, here are some options:
Array.prototype.find()
method.Keep in mind that these alternatives might not be suitable for all scenarios, and performance results may vary depending on specific use cases and environments.