Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
comparedThem
(version: 10)
Comparing performance of:
filter vs for loop
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
filter
const filterDatass = { lifeStyle:{ data: ["pescatarian"], extra: {excercise: [5,10]} }, professional: { data: ["ged"], extra: [] } }; const users = [ { firstName: "Jane", lifeStyle: { data: ["vegetarian"], otherData: {excercise: [5,10]}, }, professional: { data: ["bachelors"], otherData: [], } }, { firstName: "John", lifeStyle: { data: ["pescatarian"], otherData: {excercise: [5,10]}, }, professional: { data: ["ged"], otherData: [], } }, { firstName: "Mike", lifeStyle: { data: ["active", "pescatarian"], otherData: {excercise: [5,10]}, }, professional: { data: ["ged"], otherData: [], } }, { firstName: "Sam", lifeStyle: { data: ["vegetarian"], otherData: [], }, professional: { data: [], otherData: ["ged"], } }, ] const usersData = users.filter(function(x) { for (let item in filterDatass) { //console.log('filterDatass[item]?.extra?.length', filterDatass[item]?.extra) if (x[item]?.data.includes(...filterDatass[item]?.data)) { // console.log('filterDatass[item]?.extra !== [] && filterDatass[item]?.extra !== undefined', filterDatass[item]?.extra !== [] && filterDatass[item]?.extra !== undefined) if (filterDatass[item]?.extra !== [] || filterDatass[item]?.extra !== undefined) { //console.log('Array.isArray(filterDatass[item]?.extra) === false', filterDatass[item]?.extra) for(let items in filterDatass[item]?.extra) { if (x[item]?.otherData[items]?.length > 0) { if ( x[item]?.otherData[items][0] >= filterDatass[item]?.extra[items][0] && x[item]?.otherData[items][1] <= filterDatass[item]?.extra[items][1] ) { return x } } } } else { return x } } }})
for loop
const filterDatass = { lifeStyle:{ data: ["pescatarian"], extra: {excercise: [5,10]} }, professional: { data: ["ged"], extra: [] } }; const users = [ { firstName: "Jane", lifeStyle: { data: ["vegetarian"], otherData: {excercise: [5,10]}, }, professional: { data: ["bachelors"], otherData: [], } }, { firstName: "John", lifeStyle: { data: ["pescatarian"], otherData: {excercise: [5,10]}, }, professional: { data: ["ged"], otherData: [], } }, { firstName: "Mike", lifeStyle: { data: ["active", "pescatarian"], otherData: {excercise: [5,10]}, }, professional: { data: ["ged"], otherData: [], } }, { firstName: "Sam", lifeStyle: { data: ["vegetarian"], otherData: [], }, professional: { data: [], otherData: ["ged"], } }, ] const usersData = [] for (let i=0; i<users?.length; i++) { // console.log('users', users[i]) for (let item in filterDatass) { //console.log('filterDatass[item]?.extra?.length', filterDatass[item]?.extra) if (users[i][item]?.data.includes(...filterDatass[item]?.data)) { // console.log('filterDatass[item]?.extra !== [] && filterDatass[item]?.extra !== undefined', filterDatass[item]?.extra !== [] && filterDatass[item]?.extra !== undefined) if (filterDatass[item]?.extra !== [] || filterDatass[item]?.extra !== undefined) { // console.log('Array.isArray(filterDatass[item]?.extra) === false', filterDatass[item]?.extra) for(let items in filterDatass[item]?.extra) { if (users[i][item]?.otherData[items]?.length > 0) { if ( users[i][item]?.otherData[items][0] >= filterDatass[item]?.extra[items][0] && users[i][item]?.otherData[items][1] <= filterDatass[item]?.extra[items][1] ) { usersData.push(users[i]) } } } } else { return usersData.push(users[i]) } }}}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter
1268665.6 Ops/sec
for loop
1186549.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of two different approaches for filtering data in an array: using a `filter()` method and using a `for` loop. **What is being tested?** The benchmark tests the execution time of these two approaches on a dataset of user objects. The dataset contains 10 user objects, each with multiple properties (e.g., `firstName`, `lifeStyle`, etc.). The filtering criteria are as follows: * In the `filter()` method approach, the filter is applied to the entire array using the `includes()` method, which checks if a specific value exists in an array. This is repeated for each property in the user object. * In the `for` loop approach, a separate filter is applied for each property using a `for` loop that iterates over the properties of the user object and checks if the corresponding value exists in the `filterDatass` object. **Performance Comparison** The benchmark results show the execution time per second for both approaches on a Chrome 125 browser running on a Mac OS X 10.15.7 device. The results indicate that: * The `for` loop approach is faster than the `filter()` method approach, with an average execution time of 1186 executions per second compared to 1268 executions per second. * However, it's worth noting that the difference in performance is relatively small, and both approaches are reasonably efficient. **Code Analysis** The code for the benchmark can be summarized as follows: 1. Define a dataset of user objects with multiple properties. 2. Create a `filterDatass` object that contains filtering criteria (e.g., `data`, `otherData`) for each property. 3. Implement the `for` loop approach: * Iterate over the properties of the user object using a `for` loop. * For each property, apply a filter to check if the corresponding value exists in the `filterDatass` object. * If the value exists, push the filtered user object to an array (`usersData`). 4. Implement the `filter()` method approach: * Apply a filter to the entire array using the `includes()` method for each property. * Repeat this process for all properties in the user object. **Best Practices and Advice** Based on the benchmark results, it appears that the `for` loop approach is slightly faster than the `filter()` method approach. However, the difference in performance is relatively small, and both approaches are reasonable choices depending on the specific use case. To improve performance in this scenario: * Consider using a more efficient filtering algorithm, such as `Array.prototype.filter()`, which can take advantage of JavaScript's built-in optimizations. * Optimize the dataset size and structure to reduce the number of iterations required for filtering. * Profile and benchmark different approaches to determine the best solution for your specific use case.
Related benchmarks:
Lodash isEqual with sort vs Lodash difference
isEqual + sort vs. difference
Intl.Collator.compare vs localeCompare
Intl.Collator.compare vs localeCompare 2
Intl.Collator.compare vs localeCompare with numbers
Comments
Confirm delete:
Do you really want to delete benchmark?