Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs for loop
(version: 0)
Comparing performance of:
findIndex vs for loop vs sort via findindex vs sort via for
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
var prop7Index = items.findIndex(item => item.prop7); var prop3Index = items.findIndex(item => item.prop3); return prop7Index + prop3Index
for loop
var i = 0; var item; var length = items.length; var prop7Index var prop3Index for (; i < length; i++) { if (items[i].prop7) { prop7Index = i; } if (items[i].prop3) { prop3Index = i; } if (prop7Index !== undefined && prop3Index !== undefined) { return prop7Index + prop3Index } }
sort via findindex
return items.sort((a, b) => { const indexOfA = keys.findIndex(key => a[key]); const indexOfB = keys.findIndex(key => b[key]); return indexOfA - indexOfB || a.zIndex - b.zIndex; });
sort via for
var length = keys.length - 1; return items.sort( (left, right) => { for (let index = 0; index < length; index++) { if (Number(left[keys[index]]) - Number(right[keys[index]])) { return left.zIndex - right.zIndex; } } return left.zIndex - right.zIndex; } );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
findIndex
for loop
sort via findindex
sort via for
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):
It looks like you're reviewing benchmark results! After reviewing the data, I notice that: * The test case with the highest number of executions per second is "findIndex" (10001784.0), followed by "for loop" (8590136.0). * The test cases with lower numbers are "sort via findindex" (3273.981689453125) and "sort via for" (2860.845458984375). However, I don't see any specific analysis or conclusions drawn from these results. If you'd like, I can provide some general insights based on these benchmark results: * The "findIndex" test case seems to be the fastest, suggesting that using the `findIndex` method is more efficient than iterating through the array in a loop. * The difference between the top two tests (10001784.0 vs 8590136.0) might indicate some overhead or inefficiency in the "for loop" implementation. Would you like me to help with anything else, such as analyzing these results further or suggesting potential improvements?
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
Object arrays: findIndex vs for loop (length cached)
Comments
Confirm delete:
Do you really want to delete benchmark?