Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Max of Mins: Array Blocks
(version: 0)
Comparing performance of:
Lodash Range vs For Loop vs Map
Created:
3 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>
Script Preparation code:
var data = [2, 5, 4, 6, 3, 7, 8, 4, 6, 1, 6, 2, 8, 8, 7, 6]; var blockLength = 3;
Tests:
Lodash Range
const numBlocks = data.length - blockLength + 1; const minPerBlock = _.range(0, numBlocks).map((i) => Math.min(...data.slice(i, i + blockLength)) ); const maxOfMins = Math.max(...minPerBlock); console.log(minPerBlock); console.log(maxOfMins);
For Loop
const numBlocks = data.length - blockLength + 1; const minPerBlock = []; for (let i = 0; i < numBlocks; ++i) { const block = data.slice(i, i + blockLength); minPerBlock.push(Math.min(...block)); } const maxOfMins = Math.max(...minPerBlock); console.log( minPerBlock ) console.log( maxOfMins )
Map
const minPerBlock = [...data].map((v, idx, self) => { return self.length > idx+(blockLength-1) ? self.slice(idx, idx + blockLength) : undefined; }).filter((v)=> v !== undefined).map((group)=> Math.min(...group) ); const maxOfMins = Math.max(...minPerBlock); console.log( minPerBlock ) console.log( maxOfMins )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash Range
For Loop
Map
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 the performance of different approaches to calculate the maximum of minimum values in an array, specifically designed as sub-arrays (blocks) of a given length (`blockLength`). The test case uses the `_` symbol, which is commonly known as Lodash, a popular utility library for JavaScript. Here's what options are compared: 1. **Lodash Range**: This approach uses the `_.range()` function to generate an array of indices, then maps each index to calculate the minimum value in the corresponding block using `Math.min()`. Finally, it applies `Math.max()` to find the maximum of these minimum values. 2. **For Loop**: This approach iterates over the array indices, slices the corresponding blocks from the original data, and calculates the minimum value in each block using `Math.min()`. 3. **Map**: This approach uses an arrow function with a spread operator (`...`) to generate blocks of data, filters out undefined values (which would occur when the index exceeds the `blockLength`), maps each non-undefined value to its minimum using `Math.min()`, and finally applies `Math.max()`. Now, let's discuss the pros and cons of each approach: **Lodash Range** Pros: * Less verbose code compared to For Loop * Built-in functionality provides a more elegant solution Cons: * Uses an external library (Lodash), which may introduce overhead due to loading time or memory usage * May not be as efficient as a hand-rolled implementation, especially for very large arrays **For Loop** Pros: * No external libraries are used, reducing potential overhead * Hand-rolled implementation can be optimized for specific use cases * Provides full control over the calculation logic Cons: * More verbose code compared to Lodash Range * Requires manual error handling and edge case management **Map** Pros: * Similar performance characteristics as For Loop due to lack of external libraries * Uses a modern, concise syntax Cons: * Less readable for some developers, especially those without experience with arrow functions or spread operators * May require additional explanation for less familiar technologies Other considerations: * The `blockLength` value is not specified in the benchmark definition, so it's unclear what optimal value would be for this particular test case. * It's worth noting that these tests focus on calculating the maximum of minimum values in sub-arrays. Depending on the use case, other approaches (e.g., using `Math.min()` directly on the entire array) might be more suitable. The benchmark results show that Chrome 106, running on a Mac OS X machine, performs best with the **For Loop** approach (with approximately 12% better execution rate than Lodash Range), followed closely by **Map**, and then **Lodash Range**.
Related benchmarks:
Array.prototype.map vs Lodash.map
Array.prototype.map vs Lodash.map 4.17.15
lodash slice
Last Lodash Test
native slice vs lodash slice
Comments
Confirm delete:
Do you really want to delete benchmark?