Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs for loop simple 2
(version: 0)
Comparing performance of:
findIndex vs for loop vs for of
Created:
4 years ago
by:
Registered User
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
for (let i = 0; i < items.length; i++) { if (items[i].prop7) { return i } }
for of
let i = 0 for (let item of items) { if (item.prop7) { return i } i++ }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
findIndex
for loop
for of
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 its components. **Benchmark Definition:** The benchmark is designed to measure the performance of three different ways to iterate through an array of objects in JavaScript: 1. **findIndex**: Using the `Array.prototype.findIndex()` method to find the index of a specific property (`prop7`) in the array. 2. **For loop**: Implementing a traditional for loop to iterate through the array and return the index of the first element that matches the condition (`item.prop7`). 3. **For of loop**: Using the `for...of` loop syntax to iterate through the array and return the index of the first element that matches the condition. **Benchmark Definition Details:** The benchmark definition is a function that returns one of the three iteration methods (findIndex, For loop, or For of). The function takes no arguments and simply calls the selected method on the `items` array. **Html Preparation Code:** There is no HTML preparation code provided, which means that the benchmark assumes a headless environment where only JavaScript execution is performed. **Individual Test Cases:** The benchmark defines three test cases: 1. **findIndex**: This test case uses the `Array.prototype.findIndex()` method to find the index of the first element with a `prop7` property. 2. **For loop**: This test case implements a traditional for loop to iterate through the array and return the index of the first element that matches the condition (`item.prop7`). 3. **For of loop**: This test case uses the `for...of` loop syntax to iterate through the array and return the index of the first element that matches the condition. **Latest Benchmark Results:** The latest benchmark results show the execution counts per second for each test case: * **findIndex**: 8,240,218 executions per second * **For of loop**: 2,151,336 executions per second * **For loop**: 820,045 executions per second **Performance Comparison:** Based on the latest benchmark results, it appears that: * The `for...of` loop is the fastest method, executing approximately 10 times faster than the other two methods. * The `findIndex` method is faster than the traditional for loop, but still slower by a factor of about 12. Keep in mind that these results may vary depending on the specific JavaScript engine, browser, and environment used to run the benchmark.
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?