Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs for loop 2
(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; 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):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The test compares the performance of two approaches: 1. `findIndex` method: Using the built-in `Array.prototype.findIndex()` method to find an item in the `items` array that matches the condition. 2. For loop: Implementing a manual for loop to iterate through the `items` array and return the index of the first matching element. **Library Used** The test case uses the Xoroshiro128Plus pseudorandom number generator, which is a widely used algorithm for generating high-quality random numbers. The library is not specifically related to the benchmarking test itself, but rather provides a random number generator that's used to populate the `items` array. **Comparison of Approaches** The two approaches being compared are: 1. **findIndex**: This method uses the built-in JavaScript function to find an item in the array that matches the given condition. It's a concise and efficient way to perform this operation. 2. **For Loop**: This approach involves implementing a manual for loop to iterate through the `items` array, checking each element against the condition, and returning the index of the first matching element. **Pros and Cons** Here are some pros and cons of each approach: 1. **findIndex** * Pros: + Concise and efficient. + Built-in function, so it's likely to be well-optimized. * Cons: + May not be as educational or insightful for understanding performance optimizations. 2. **For Loop** * Pros: + Can provide insights into performance optimization techniques, such as indexing and caching. + May be more educational for developers who want to understand the underlying mechanics of array iteration. * Cons: + More verbose and less efficient than the built-in `findIndex()` method. **Performance Considerations** The performance results show that the `findIndex` method is significantly faster than the manual for loop approach, with a ratio of approximately 2.7:1. This is likely due to the optimized implementation of the built-in function, which can take advantage of various caching and indexing techniques. However, it's worth noting that the performance difference may not be significant enough to justify using the `findIndex()` method in all cases. If educational value or insight into performance optimizations are important, the for loop approach may still be a good choice. In summary, the benchmark compares two approaches to finding an item in an array: the built-in `Array.prototype.findIndex()` method and a manual for loop implementation. The results show that the `findIndex` method is significantly faster due to its optimized implementation.
Related benchmarks:
Dot vs IndexOf 9
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
Comments
Confirm delete:
Do you really want to delete benchmark?