Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Searching in arrays
(version: 0)
Comparing performance of:
for vs for-of vs array.find
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (let i = 0; i < 1000; i++) { arr.push(i); }
Tests:
for
for (let i = 0; i < arr.length; i++) { if (arr[i] == 342) { return true; } }
for-of
for (num of arr) { if (num == 342) { return true; } }
array.find
arr.find((num) => num == 342);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
for-of
array.find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 16 on iOS 16.6
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
51104.5 Ops/sec
for-of
23230.9 Ops/sec
array.find
2069054.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Definition** The benchmark definition represents a JavaScript script that performs a specific operation on an array. In this case, there are three test cases: 1. `for`: uses a traditional `for` loop to iterate over the array. 2. `for-of`: uses the `of` keyword with a `for...of` loop to iterate over the array. 3. `array.find`: uses the `find()` method of the Array prototype to search for a specific value in the array. **Options being compared** The three options are being compared to determine which one is the most efficient. * **Pros and Cons:** + `for` loop: - Pros: widely supported, easy to understand. - Cons: can be slower than other methods for large datasets. + `for-of` loop: - Pros: more modern and concise syntax, potentially faster execution. - Cons: may not work in older browsers or environments that don't support the `of` keyword. + `array.find()`: uses the Array.prototype method to search for a value. - Pros: concise and expressive syntax, often faster than traditional loops for large datasets. - Cons: may have additional overhead due to the use of the Array.prototype method. **Library usage** None of the test cases explicitly use any external libraries. **Special JS features or syntax** The `for-of` loop option uses a feature called "structured bindings" which is a more modern and concise way of iterating over arrays. This feature was introduced in ECMAScript 2015 (ES6) and has since been widely adopted by modern browsers. **Other alternatives** Some other alternatives to the above options could be: * Using `forEach()` method: `arr.forEach((num) => { if (num == 342) return true; });` * Using a while loop: `let i = 0; while(i < arr.length && arr[i] != 342) {i++};` * Using regular expressions: `var result = arr.some(function(num) { return num == 342; });` However, these alternatives may not be as concise or efficient as the above options. **Device and browser context** The benchmark results are reported for a specific device (iPhone with iOS 16.6) and browser (Mobile Safari 16). The results may not be representative of other devices or browsers, but they provide insight into how each option performs on this specific platform.
Related benchmarks:
Preinitialized array size vs Push operations to an empty one.
empty an array in JavaScript?(Yorkie)1
Array spread vs push 2
Test array and unshift
+ vs Number, with 100k numbers
Comments
Confirm delete:
Do you really want to delete benchmark?