Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained RR3 (100000)
(version: 0)
Comparing performance of:
for loop vs chained arr
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const randomStDate = () => { const randD = new Date(new Date("2016-12-05").getTime() + Math.random() * (new Date("2030-12-05").getTime() - new Date("2023-12-05").getTime())) let month = '' + (randD.getMonth() + 1); let day = '' + randD.getDate(); let year = randD.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('-'); }; const rndId = Math.floor(Math.random() * (100000000 - 1 + 1) + 1) const createArr = () => { let workArr = [] for (let i = 0; i < 100000; i++) { const pl = { tisId: rndId, startDate: randomStDate(), endDate: "2026-12-05", site: "Liverpool", } workArr.push(pl) } return workArr } var workArr = createArr() var today = new Date().toISOString().slice(0, 10)
Tests:
for loop
let pastArr = []; let futureArr = []; for (let i = 0; i < workArr.length; i++) { const pl = workArr[i]; if (pl.startDate <= today) { pastArr.push(pl); } else if (futureArr.length < 1 && pl.startDate > today) { futureArr = [pl]; } else if (futureArr.length > 0 && pl.startDate < futureArr[0].startDate) { futureArr = [pl]; } else if (futureArr.length > 0 && pl.startDate === futureArr[0].startDate) { futureArr.push(pl); } } return [...pastArr, ...futureArr]
chained arr
const firstFutureWorks = workArr .filter(w => w.startDate > today) .sort((a, b) => (a.startDate > b.startDate ? 1 : -1)); const nextFutureDate = firstFutureWorks[0] ? firstFutureWorks[0].startDate : today; return workArr.filter(w => w.startDate <= nextFutureDate);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
chained arr
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):
Measuring JavaScript performance is crucial for building efficient and scalable applications. The provided benchmark measures the execution speed of two approaches to process an array of objects: 1. **For Loop**: This approach uses a traditional `for` loop to iterate through the array and filter out elements based on their start dates. 2. **Chained Arr**: This approach uses the `filter()` method in conjunction with the `sort()` method to achieve the same filtering result. Let's break down each approach: **For Loop** Pros: * Easy to understand and implement for those familiar with traditional loops. * Can be more intuitive for simple filtering tasks. Cons: * May have performance overhead due to the loop itself, especially for large datasets. * Requires manual handling of array bounds and edge cases. **Chained Arr** Pros: * Often faster than traditional loops due to the optimized implementation of `filter()` and `sort()`. * Encourages a more functional programming style, making code more concise and expressive. Cons: * May be less intuitive for those unfamiliar with functional programming concepts. * Requires careful consideration of edge cases, such as empty arrays or duplicate dates. The benchmark uses the `filter()` method to remove elements from the array based on their start date, and then sorts the remaining elements to ensure they are in chronological order. The resulting filtered array is returned. **Library and Special JS Feature** In this benchmark, the following library is used: * **Array.prototype.filter()**: A built-in JavaScript method that returns a new array with all elements that pass the test implemented by the provided function. * **Array.prototype.sort()**: A built-in JavaScript method that sorts the elements of an array in place and returns the array. No special JS features or syntax are used in this benchmark, making it accessible to developers with varying levels of experience. **Other Alternatives** If you're looking for alternative approaches to process arrays in a performance-critical section of your code, consider: * **Using `reduce()`**: Instead of filtering and sorting the array, you can use `reduce()` to accumulate the results. * **Employing a library like Lodash**: If you need more advanced filtering and sorting capabilities, libraries like Lodash provide a range of utility functions that can simplify your code. * **Exploring WebAssembly (WASM)**: If you're targeting a specific browser or platform, consider using WASM to run optimized, compiled JavaScript code. Keep in mind that the best approach will depend on the specific requirements and constraints of your project.
Related benchmarks:
fight to the death - for loop vs chained array methods (10)
fight to the death - for loop vs chained methods 1,000,000
fight to the death - for loop vs chained RR2 (100000)
fight to the death - for loop vs chained RR (1000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?