Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained array methods (10)
(version: 3)
arr length 10 (replaced if else with a switch for the win?)
Comparing performance of:
for loop with switch vs chained arr methods
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()
Tests:
for loop with switch
const today = new Date().toISOString().slice(0,10) 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]
chained arr methods
const today = new Date().toISOString().slice(0,10) 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 with switch
chained arr methods
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 break down the provided JSON and explain what's being tested in this JavaScript microbenchmark. **Benchmark Definition** The benchmark is defined by two individual test cases: 1. **"For Loop with Switch"`**: This test case compares the performance of using a traditional `for` loop with a `switch` statement to iterate over an array. 2. **"Chained Array Methods"`**: This test case compares the performance of using chained array methods (e.g., `filter()`, `sort()`) to process an array. **Options Compared** For each test case: 1. **"For Loop with Switch"`**: * Traditional `for` loop with a `switch` statement. * No alternatives are explicitly mentioned, but in general, other approaches could be used, such as using `forEach()` or `map()`. 2. **"Chained Array Methods"`**: * Chaining array methods (e.g., `filter()`, `sort()`) to process an array. * Alternative approaches might include using a single `for` loop with array iteration, or using other library functions like `Array.prototype.reduce()`. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: 1. **"For Loop with Switch"`**: * Pros: Simple, straightforward, and easy to understand. * Cons: Can be slower than chained array methods due to overhead from the `switch` statement. 2. **"Chained Array Methods"`**: * Pros: Typically faster and more concise, making it a popular choice for array processing tasks. * Cons: Can lead to deeper function calls, potentially increasing memory usage or performance overhead. **Library Usage** In this benchmark, no specific libraries are mentioned, but the use of `Array.prototype.filter()`, `Array.prototype.sort()`, and other chained methods suggests that standard JavaScript Array functions are being utilized. No external libraries like Lodash or Ramda are required for these test cases. **Special JS Features/Syntax** This benchmark does not explicitly mention any special JavaScript features or syntax, such as async/await, arrow functions, or generators. **Other Alternatives** If you're interested in exploring alternative approaches, here are some options: * For `for Loop with Switch`: + Using `forEach()` or `map()` + Implementing a custom iteration mechanism using recursion or closures * For "Chained Array Methods": + Using a single `for` loop with array iteration + Employing other library functions like `Array.prototype.reduce()`, `Array.prototype.every()`, etc. + Considering more advanced techniques, such as parallel processing using Web Workers or `async/await` Please note that these alternatives might not necessarily be equivalent in performance to the original test cases, and may require additional considerations for correctness and maintainability.
Related benchmarks:
fight to the death - for loop vs chained array methods (100,000)
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 1,000,000
Comments
Confirm delete:
Do you really want to delete benchmark?