Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained (10) - 2
(version: 0)
Comparing performance of:
chained arr vs for loop
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const randomStDate = () => { const randD = new Date(new Date("2021-12-05").getTime() + Math.random() * (new Date("2026-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 < 10; 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:
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);
for loop
let pastArr = []; let futureArr = []; for (let i = 0; i < workArr.length; i++) { const pl = workArr[i]; switch(true){ case pl.startDate <= today : pastArr.push(pl); break; case futureArr.length < 1 && pl.startDate > today : futureArr = [pl]; break; case futureArr.length > 0 && pl.startDate < futureArr[0].startDate : futureArr = [pl]; break; case futureArr.length > 0 && pl.startDate === futureArr[0].startDate : futureArr.push(pl); break; } } return [...pastArr, ...futureArr]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
chained arr
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 world of JavaScript microbenchmarks! The provided benchmark measures two different approaches to filter and sort an array of objects in JavaScript: 1. **Chained Array Method**: * The script preparation code creates an array `workArr` with 10 objects, each containing a random start date. * The HTML preparation code is empty, which means no HTML elements are used in this benchmark. * The individual test case uses the following JavaScript code: ```javascript 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); ``` This method uses the `filter()` and `sort()` array methods, which are chained together to achieve the desired result. 2. **For Loop Method**: * The script preparation code is similar to the first method. * The individual test case uses the following JavaScript code: ```javascript let pastArr = []; let futureArr = []; for (let i = 0; i < workArr.length; i++) { const pl = workArr[i]; switch(true){ case pl.startDate <= today : pastArr.push(pl); break; case futureArr.length < 1 && pl.startDate > today : futureArr = [pl]; break; case futureArr.length > 0 && pl.startDate < futureArr[0].startDate : futureArr = [pl]; break; case futureArr.length > 0 && pl.startDate === futureArr[0].startDate : futureArr.push(pl); break; } } return [...pastArr, ...futureArr]; ``` This method uses a `for` loop to iterate over the array and apply conditional logic to separate the objects into two arrays: `pastArr` and `futureArr`. Now, let's discuss the pros and cons of each approach: **Chained Array Method** Pros: * Concise and readable code * Uses built-in array methods, which are optimized for performance Cons: * May not be as efficient as a custom implementation due to the overhead of function calls and method lookups * Can be slower for very large arrays due to the overhead of sorting **For Loop Method** Pros: * Can be more control-oriented, allowing developers to optimize specific parts of the code * Can be faster for very large arrays since it avoids the overhead of built-in array methods Cons: * More verbose and harder to read than the chained array method * May require additional logic to handle edge cases correctly Other considerations: * The use of a `switch` statement in the for loop method can lead to slower execution due to its inherent overhead. * The chaining of filter() and sort() methods is generally considered good practice, as it allows for easy modification and testing of individual parts of the code. Alternatives to these approaches include: * Using a custom implementation with hand-rolled loops and conditional logic * Utilizing libraries like Lodash or Ramda, which provide optimized functions for common tasks like filtering and sorting * Leveraging modern JavaScript features like `Array.prototype.includes()` and `Array.prototype.find()` to simplify the code Keep in mind that the best approach depends on the specific requirements of your project, including performance, readability, and maintainability considerations.
Related benchmarks:
fight to the death - for loop vs chained array methods (10)
fight to the death - for loop vs chained methods1
fight to the death - for loop vs chained methods1 array length 100
fight to the death - for loop vs chained methods 10,000
fight to the death - for loop vs chained methods 1,000,000
Comments
Confirm delete:
Do you really want to delete benchmark?