Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex update vs for loop update edited 1
(version: 2)
Comparing performance of:
update via findIndex vs for loop update
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = new Array(2000000); arr.fill({ id: 0 }); arr = arr.map((el, idx) => el.id = idx); var findItemIndex = (idToFind, list) => { var index = -1 for (var i = 0; i < list.length; i++) { if (list[i].id === idToFind) { index = i; break; } } return index; } var itemsToUpdate = [{ id: 100, updates: { name: "111" } }, { id: 200, updates: { name: "111" } }, { id: 400, updates: { name: "111" } }, { id: 600, updates: { name: "111" } }, { id: 800, updates: { name: "111" } }]
Tests:
update via findIndex
itemsToUpdate.forEach((t) => { const storedIndex = arr.findIndex((tD) => { return tD.id === t.id }) if(storedIndex >= 0){ arr[storedIndex] = { ...arr[storedIndex], ...t.updates } } })
for loop update
itemsToUpdate.forEach((t) => { const storedIndex = findItemIndex(t.id, arr) if(storedIndex >= 0){ arr[storedIndex] = { ...arr[storedIndex], ...t.updates } } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
update via findIndex
for loop update
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
update via findIndex
21.2 Ops/sec
for loop update
167.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark definition represents two different approaches to update elements in an array: 1. **For loop update**: The first approach uses a for loop to iterate over the `itemsToUpdate` array and updates each element in the `arr` array using the `findIndex` method. 2. **Update via findIndex**: The second approach uses the `findItemIndex` function to find the index of each element in the `arr` array and then updates the corresponding element using the `findIndex` method. **Options Compared** The two options being compared are: * For loop update * Update via findIndex **Pros and Cons** 1. **For loop update**: * Pros: Generally faster and more efficient, as it avoids the overhead of the `findItemIndex` function. * Cons: Can be slower for large arrays, as it requires iterating over each element individually. 2. **Update via findIndex**: * Pros: More concise and elegant, as it uses a single function to perform both finding and updating operations. * Cons: May be slower due to the overhead of the `findItemIndex` function. **Other Considerations** * The benchmark is using a large array of 2 million elements for testing. * Both approaches assume that the element being updated has an `id` property that can be used for lookup. * The `itemsToUpdate` array contains five elements with identical `updates` objects, which may affect the performance results. **Library and Special JS Feature** The benchmark is using the following library: * None explicitly mentioned, but it's likely that the JavaScript engine being tested (e.g., V8 in Chrome) is using its built-in implementation of the `findIndex` method. There are no special JavaScript features or syntax used in this benchmark beyond what's standard for JavaScript. **Alternatives** Other alternatives for updating elements in an array include: * Using `Array.prototype.map()` with a callback function, which can be more concise but may have performance implications. * Using `Array.prototype.forEach()` with a callback function, which can provide better error handling and debugging capabilities. * Implementing custom iteration logic using `for...of` loops or recursive functions. However, the for loop update and update via findIndex approaches are likely to be among the most common and efficient methods used in practice.
Related benchmarks:
map vs findIndex & slice
update object at certain index in array .map() vs .slice()
Update Or Insert - splice vs slice
simple array and map vs forEach vs for by cuteLuna v4
Comments
Confirm delete:
Do you really want to delete benchmark?