Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs for loop simple
(version: 0)
Comparing performance of:
findIndex vs for loop
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function Xoroshiro128Plus(options) { let { state } = options || {}; if (!state) { let time = (Date.now() | 0) >>> 0; state = [ (time & ((Math.random() * 0xffffffff) | 0)) >>> 0, (time & ((Math.random() * 0xffffffff) | 0)) >>> 0, (time & ((Math.random() * 0xffffffff) | 0)) >>> 0, (time & ((Math.random() * 0xffffffff) | 0)) >>> 0, ]; } this.next64 = () => xoroshiro128plus(state); this.getState = () => state.slice(); } function xoroshiro128plus(state) { let s0 = [state[0], state[1]]; let s1 = [state[2], state[3]]; let result = sum64(s0, s1); s1 = xor64(s1, s0); let [a, b] = xor64( xor64(or64(shiftLeft64(s0, 55), shiftRight64(s0, 9)), s1), shiftLeft64(s1, 14), ); let [c, d] = or64(shiftLeft64(s1, 36), shiftRight64(s1, 28)); state[0] = a; state[1] = b; state[2] = c; state[3] = d; return result; function shiftLeft64([x, y], count) { // `32` case is not handled if (count < 32) { x = x << count; let a = (y >>> (32 - count)) & (Math.pow(2, count) - 1); x += a; return [x >>> 0, (y << count) >>> 0]; } else { x = (y & (Math.pow(2, 64 - count) - 1)) << (count - 32); return [x >>> 0, 0]; } } function shiftRight64([x, y], count) { // `32` case is not handled if (count < 32) { let a = x & (Math.pow(2, count) - 1); y = (y >>> count) + (a << (32 - count)); return [x >>> count, y >>> 0]; } else { let a = (x >> (count - 32)) & (Math.pow(2, 64 - count) - 1); x = (x >> (count - 32)) & (Math.pow(2, 64 - count) - 1); x = x + (a << (count - 32)); return [0, x >>> 0]; } } function or64([a0, a1], [b0, b1]) { return [(a0 | b0) >>> 0, (a1 | b1) >>> 0]; } function xor64([a0, a1], [b0, b1]) { return [(a0 ^ b0) >>> 0, (a1 ^ b1) >>> 0]; } function sum64([a0, a1], [b0, b1]) { let [y, overflow] = sum32(a1, b1); let x = (a0 + b0 + (overflow ? 1 : 0)) >>> 0; return [x, y]; } function sum32(a, b) { let overflow = false; let max = 0xffffffff; max -= a; if (b > max) { overflow = true; } return [(a + b) >>> 0, overflow]; } } var random = new Xoroshiro128Plus({ state: [1292125120, -349674120, 855278889, 359289241] }); var ELEMENTS_COUNT = 1000; var FIELDS_COUNT = 10; var items = []; for (let i = 0; i < ELEMENTS_COUNT; i++) { var [zIndex, bits] = random.next64(); var obj = { zIndex }; for (let j = 1; j <= FIELDS_COUNT; j++) { obj[`prop${j}`] = Boolean((bits >> (j - 1)) & 1); } items.push(obj); } var keys = [ "prop1", "prop2", "prop3", "prop4", "prop5", "prop6", "prop7", "prop8", "prop9", "prop10", ];
Tests:
findIndex
return items.findIndex(item => item.prop7)
for loop
var i = 0; var length = items.length; for (; i < length; i++) { if (items[i].prop7) { return i; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
for loop
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):
Measuring the performance of different JavaScript algorithms is crucial in optimizing code execution time. The provided benchmark, MeasureThat.net, allows users to compare the performance of two approaches: using the built-in `findIndex()` method and implementing a custom loop-based search. **FindIndex() Method** The `findIndex()` method returns the index of the first element that satisfies the provided testing function. In this case, the testing function is `item => item.prop7`. * **Description**: The `findIndex()` method uses a linear search algorithm to find the target element in the array. * **Pros**: * Efficient and concise way to perform binary searches in JavaScript arrays. * Less prone to errors, as it automatically handles edge cases like empty arrays or arrays with only one element. * **Cons**: * May not be suitable for very large datasets due to its linear search nature. * Can lead to unnecessary iterations if the array is not sorted. **Custom Loop-Based Search** The second test case involves a custom loop-based search using the following code: ```javascript var i = 0; var length = items.length; for (; i < length; i++) { if (items[i].prop7) { return i; } } ``` * **Description**: This is an iterative approach that manually checks each element in the array until it finds the target property (`prop7`). * **Pros**: * Can be more efficient for very large datasets since it does not rely on binary search. * Provides direct insight into the algorithm's logic and potential optimization points. * **Cons**: * More error-prone due to manual iteration and boundary checks. **Benchmark Results** The provided benchmark results show the execution times for both `findIndex()` and custom loop-based search methods. The Chrome Mobile 96 browser yields the following execution times: | Test Name | Execution Time (Sec) | | --- | --- | | findIndex | 3.47286625 | | for loop | 34.7286 | The `findIndex()` method is significantly faster, indicating that this approach might be more efficient in general use cases. **Conclusion** When deciding between the built-in `findIndex()` method and a custom loop-based search, consider the trade-offs: * **Conciseness and maintainability**: Use `findIndex()` for most cases due to its simplicity and reduced chance of errors. * **Large dataset handling**: If dealing with massive datasets, a custom loop-based approach might be more suitable due to its potential efficiency benefits. Keep in mind that these results are specific to the given benchmark scenario and may not apply universally.
Related benchmarks:
Object arrays: findIndex vs forOf loop
object[]: findIndex vs for loop with more complex condition check
Object arrays: findIndex vs for loop2
some vs. findIndex vs find
Object arrays: findIndex vs for loop (length cached)
Comments
Confirm delete:
Do you really want to delete benchmark?