Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash find vs for loop 2
(version: 0)
Comparing performance of:
Lodash Find vs For loop
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
Lodash Find
let field = "connection_status"; let gridColumn = [ { headerName: "Participant", field: "companyname" }, { headerName: "Participant", field: "companyname" }, { headerName: "Line Id", field: "sendercomp" }, { headerName: "User Type", field: "user_type" }, { headerName: "User Id", field: "user_id" }, { headerName: "Crd", field: "crd" }, { headerName: "Source IP", field: "source_ip" }, { headerName: "Source Port", field: "source_port" }, { headerName: "Last Connection", field: "last_connection" }, { headerName: "Pri IP", field: "pri_ip" }, { headerName: "Pri Module", field: "pri_module" }, { headerName: "Sec IP", field: "sec_ip" }, { headerName: "Sec Module", field: "sec_module" }, { headerName: "Sod", field: "sodcount" }, { headerName: "Eod", field: "eodcount" }, { headerName: "Seq", field: "last_seq" }, { headerName: "PRN", field: "last_prn" }, { headerName: "RSeq", field: "gt_seq" }, { headerName: "GT Stream", field: "gt_stream" }, { headerName: "Pri Connected", field: "pri_connected" }, { headerName: "Sec Connected", field: "sec_connected" }, { headerName: "Port", field: "port" }, { headerName: "DR Pri IP", field: "dr_pri_ip" }, { headerName: "DR Sec IP", field: "dr_sec_ip" }, { headerName: "Total Conn", field: "n_connect" }, { headerName: "Total Conn Attempts", field: "n_conn_attempt" }, { headerName: "Session Status", field: "session_status" }, { headerName: "Connection Status", field: "connection_status" } ]; function test(gridColumn, field){ let column = _.find(gridColumn, { field: field }); return column ? column.headerName : field; }; let result = test(gridColumn, field);
For loop
let field = "connection_status"; let gridColumn = [ { headerName: "Participant", field: "companyname" }, { headerName: "Participant", field: "companyname" }, { headerName: "Line Id", field: "sendercomp" }, { headerName: "User Type", field: "user_type" }, { headerName: "User Id", field: "user_id" }, { headerName: "Crd", field: "crd" }, { headerName: "Source IP", field: "source_ip" }, { headerName: "Source Port", field: "source_port" }, { headerName: "Last Connection", field: "last_connection" }, { headerName: "Pri IP", field: "pri_ip" }, { headerName: "Pri Module", field: "pri_module" }, { headerName: "Sec IP", field: "sec_ip" }, { headerName: "Sec Module", field: "sec_module" }, { headerName: "Sod", field: "sodcount" }, { headerName: "Eod", field: "eodcount" }, { headerName: "Seq", field: "last_seq" }, { headerName: "PRN", field: "last_prn" }, { headerName: "RSeq", field: "gt_seq" }, { headerName: "GT Stream", field: "gt_stream" }, { headerName: "Pri Connected", field: "pri_connected" }, { headerName: "Sec Connected", field: "sec_connected" }, { headerName: "Port", field: "port" }, { headerName: "DR Pri IP", field: "dr_pri_ip" }, { headerName: "DR Sec IP", field: "dr_sec_ip" }, { headerName: "Total Conn", field: "n_connect" }, { headerName: "Total Conn Attempts", field: "n_conn_attempt" }, { headerName: "Session Status", field: "session_status" }, { headerName: "Connection Status", field: "connection_status" } ]; function test(gridColumn, field){ for (var i = 0, len = gridColumn.length; i < len; i++) { if (gridColumn[i].field === field) { return gridColumn[i].headerName; } } return field; }; let result = test(gridColumn, field);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash Find
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):
Let's dive into the explanation. The benchmark being tested is the performance difference between using `_.find` from Lodash and a traditional for loop to find an element in an array. **Options Compared:** 1. **Lodash Find**: Uses the `_.` namespace to call the `find` method on the `gridColumn` array, passing the target value (`field`) as an argument. 2. **For Loop**: Iterates through each element of the `gridColumn` array using a traditional for loop, checking if the current element's `field` property matches the target value. **Pros and Cons:** * **Lodash Find**: + Pros: - Faster execution time due to optimized C++ implementation. - More concise code. + Cons: - Requires Lodash library to be included in the HTML document (as shown in the benchmark setup). - May not be suitable for very large arrays or performance-critical applications. * **For Loop**: + Pros: - No external dependencies required. - Can be more intuitive for developers familiar with traditional array iteration. + Cons: - Slower execution time due to JavaScript's overhead and potential loop optimizations. - More verbose code. **Benchmark Results:** The latest benchmark results show that the **For Loop** implementation is significantly slower than the **Lodash Find** implementation, with an average of 2274949.5 executions per second vs. 386342.59375 executions per second for Chrome Mobile 81 on Android. Keep in mind that these results may vary depending on the specific use case, array size, and hardware configuration. Overall, the benchmark suggests that using Lodash's `find` method can provide a significant performance boost for this particular operation, but it's essential to consider the trade-offs, including the added dependency on an external library.
Related benchmarks:
Find item in array - Fork
Lodash find vs binary find
findIndex performance
Array find vs lodash _.find
native find vs lodash _.find equal
Comments
Confirm delete:
Do you really want to delete benchmark?