Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop vs find 2
(version: 0)
loop performance
Comparing performance of:
find vs loop vs loop without lenght inside
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
/* make a new array with 15k items */ var arr = new Array(15000); /* fill array tems with basic object */ arr.fill({ id: 0 }); /* generate 2 random numbers in our 15k range */ var foo = Math.floor(Math.random() * 15000); var bar = Math.floor(Math.random() * 15000); /* set the index of arr[foo] to foo for our tests */ arr[foo].id = foo;
Tests:
find
/* make a new array with 15k items */ var arr = new Array(15000); /* fill array tems with basic object */ arr.fill({ id: 0 }); /* generate 2 random numbers in our 15k range */ var foo = Math.floor(Math.random() * 15000); var bar = Math.floor(Math.random() * 15000); /* set the index of arr[foo] to foo for our tests */ arr[foo].id = foo;
loop
const bookFreePlatform = (arr, foo, bar) => { /* find array item where id == foo */ for (let i = 0; i < arr.length; i++) { /* not an error check, just a simple comparison */ if (arr[i].id === foo) { /* if we got here, it found and index, lets edit it and log it again */ arr[i].id = bar; console.log(arr[i]); /* stop the loop to save time and resources, we're done */ break; } } /* return altered array */ return arr; } bookFreePlatform(arr, foo, bar);
loop without lenght inside
const bookFreePlatform = (arr, foo, bar) => { let outside = arr.length; /* find array item where id == foo */ for (let i = 0; i < outside ; i++) { /* not an error check, just a simple comparison */ if (arr[i].id === foo) { /* if we got here, it found and index, lets edit it and log it again */ arr[i].id = bar; console.log(arr[i]); /* stop the loop to save time and resources, we're done */ break; } } /* return altered array */ return arr; } bookFreePlatform(arr, foo, bar);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
find
loop
loop without lenght inside
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):
Let's break down the test cases and explain what's being tested. **Overview** The benchmark compares three approaches to find an element in an array: 1. **Find with traditional loop**: This approach uses a traditional `for` loop to iterate through the array elements, comparing each element's `id` property to the target value (`foo`). 2. **Find with explicit length check**: Similar to the previous approach, but the loop condition checks the length of the array instead of using `arr.length`. 3. **Find without explicit length check (loop without length)**: This approach uses a traditional `for` loop, but skips checking the array length before entering the loop. **Library** The test case "find" uses an arrow function with two parameters (`arr`, `foo`, and `bar`). The purpose of this library is to: * Take in an array (`arr`) as input * Use the `foo` value to find a matching element in the array * Update the found element's `id` property with the `bar` value * Return the modified array **Special JS feature** The test case "loop without length inside" uses a special JavaScript feature called **let scoping**. The variable `outside` is declared using the `let` keyword, which creates a new scope for this variable. This allows the code to use the `outside` variable without polluting the global namespace. **Pros and cons of each approach** * **Find with traditional loop**: Pros: + Easy to understand and implement + Works well for small arrays Cons: + Slow for large arrays due to the unnecessary iteration * **Find with explicit length check**: Same pros and cons as the previous approach, with an added complexity due to the extra condition. * **Find without explicit length check (loop without length)**: Pros: + Fast for large arrays since it avoids unnecessary iteration Cons: + Can lead to infinite loops if the array is not correctly populated or if the target value is out of bounds + Requires careful handling of edge cases **Other alternatives** In general, modern JavaScript engines optimize array searches using techniques like: * **Cache misses**: Modern engines cache recent array access results and reuse them when possible. * **Loop unrolling**: Some compilers can unroll loops to reduce overhead. * **SIMD instructions**: Some CPU architectures support SIMD (Single Instruction, Multiple Data) instructions that can accelerate certain operations. However, these optimizations may not always be available or relevant for small-scale benchmarking like this one.
Related benchmarks:
Object arrays: findIndex vs for loop2
Object arrays: findIndex vs for loop (length cached)
Object arrays: findIndex vs for loop vs some
Object arrays: map + indexOf vs for loop vs findIndex for finding and editing an array item.
Comments
Confirm delete:
Do you really want to delete benchmark?