Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
josh testing array stuff
(version: 0)
Comparing performance of:
for loop vs for of vs array find
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.arr = new Array(100000).fill().map((x, i) => i);
Tests:
for loop
for(i=0; i<window.arr.length; i++){ var value = arr[i]; if (value === 99999) { val = value; break; } }
for of
let val; for (var value of window.arr) { if (value === 99999) { val = value; break; } }
array find
let val = window.arr.find(val => val === 99999);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for loop
for of
array find
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark definition is a JSON object that represents the test case. It includes: * `Name` and `Description`: These are arbitrary names for the benchmark. * `Script Preparation Code` and `Html Preparation Code`: These are JavaScript code snippets that need to be executed before running the actual benchmark. In this case, the script preparation code is: ```javascript window.arr = new Array(100000).fill().map((x, i) => i); ``` This code creates an array of 100,000 elements and assigns it to a global variable `arr`. **Individual Test Cases** There are three test cases: 1. **For Loop** ```javascript for(i=0; i<window.arr.length; i++){\r\n var value = arr[i];\r\n if (value === 99999) {\r\n val = value;\r\n break;\n }\r\n} ``` This test case uses a traditional `for` loop to iterate over the array and find the first element with a value of 99999. 2. **For Of** ```javascript let val; for (var value of window.arr) {\r\n if (value === 99999) {\r\n val = value;\r\n break;\n }\r\n} ``` This test case uses the `for...of` loop to iterate over the array and find the first element with a value of 99999. 3. **Array Find** ```javascript let val = window.arr.find(val => val === 99999); ``` This test case uses the `find()` method of the array to search for an element that matches the specified condition (in this case, `val === 99999`). **Library** None of these tests use a library. **Special JS Features/Syntax** * The `for...of` loop is a relatively new feature in JavaScript, introduced in ECMAScript 2015. * The `find()` method was also introduced in ECMAScript 2015. **Options Compared** The benchmark compares three different ways to find the first element with a value of 99999 in an array: 1. Traditional `for` loop 2. `For...of` loop 3. `Find()` method **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Traditional For Loop** + Pros: Widely supported, easy to understand. + Cons: Can be slower than other approaches due to the overhead of manual index management. * **For Of Loop** + Pros: More concise and expressive than traditional `for` loops. Less prone to off-by-one errors. + Cons: May not be as widely supported, especially in older browsers or environments with strict mode enabled. * **Find() Method** + Pros: Most concise and efficient way to find a single element in an array. Returns the matched value immediately. + Cons: Requires modern JavaScript features (ECMAScript 2015+) and may not be supported in older browsers. **Other Considerations** When choosing between these approaches, consider the following factors: * Performance: If performance is critical, use the `find()` method or `for...of` loop for their potential speed advantages. * Readability: Choose the most concise and expressive approach that aligns with your coding style and team's preferences. * Browser Support: Ensure that your chosen approach is supported by all target browsers. **Alternatives** If you're looking for alternative ways to find a single element in an array, consider using: * `indexOf()` method ( returns the index of the first occurrence, or -1 if not found) * Regular expressions (e.g., `Array.prototype.some()` with a regex test function) Keep in mind that these alternatives may have different trade-offs and considerations compared to the three options tested in this benchmark.
Related benchmarks:
Array constructor vs literal
Array.indexOf()
Concat vs push(...) for large arrays of arrays
Big Array prepend
Array shift vs pop
Comments
Confirm delete:
Do you really want to delete benchmark?