Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash map/max fusion 2
(version: 1)
Comparing performance of:
Lodash map then max vs Lodash map/max fusion vs Lodash sort then last vs Lodash map/max fusion with closure vs Lodash maxBy vs Lodash map then max with closure
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var data = Array(1000000).fill().map((x, i) => ({ filtering: true, mapping: 42 + i }));
Tests:
Lodash map then max
_.max(_.map(data, 'mapping'))
Lodash map/max fusion
_(data).map('mapping').max()
Lodash sort then last
_(data).sortBy('mapping').last().mapping
Lodash map/max fusion with closure
_(data).map(o => o.mapping).max()
Lodash maxBy
_.maxBy(data, 'mapping').mapping
Lodash map then max with closure
_.max(_.map(data, o=>o.mapping))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Lodash map then max
Lodash map/max fusion
Lodash sort then last
Lodash map/max fusion with closure
Lodash maxBy
Lodash map then max with closure
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 benchmark. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark created using MeasureThat.net. The benchmark tests various approaches to calculating the maximum value in an array of objects, utilizing the popular utility library Lodash. **Test Cases and Approaches** There are four main test cases: 1. **Lodash map then max**: `_.max(_.map(data, 'mapping'))` 2. **Lodash map/max fusion**: `(data).map('mapping').max()` 3. **Lodash sort then last**: `(data).sortBy('mapping').last().mapping` 4. **Lodash map/max fusion with closure**: `(data).map(o => o.mapping).max()` Each test case uses a different approach to calculate the maximum value: * Test 1: Maps over the array, extracts the mapping field, and then applies `_.max` from Lodash. * Test 2: Uses Lodash's `map` method with an arrow function that returns the mapping field, followed by the `max` method. * Test 3: Sorts the array by the mapping field using Lodash's `sortBy` method, selects the last element (using `last()`), and then extracts the mapping field. * Test 4: Maps over the array, applies an arrow function to extract the mapping field, and then applies the `max` method. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks such as: * Array manipulation (e.g., `map`, `filter`, `reduce`) * Object manipulation (e.g., `keys`, `values`, `pick`) * String manipulation (e.g., `trim`, `toUpper`, `split`) * Miscellaneous functions (e.g., `each`, `forEach`, `compact`) In this benchmark, Lodash is used to provide the `map` and `max` functions. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The tests only employ standard JavaScript programming constructs and utilize Lodash's utility functions. **Pros and Cons of Different Approaches** Here's a brief analysis of each approach: * **Lodash map then max**: Simple and effective, but may incur overhead due to the two separate function calls. * **Lodash map/max fusion**: More efficient than the first approach, as it reduces the number of function calls. However, it may be less readable for those unfamiliar with Lodash's API. * **Lodash sort then last**: This approach is not as efficient as the other two, as sorting the entire array can be costly for large datasets. **Other Alternatives** If you wanted to implement this benchmark without using a utility library like Lodash, you could use: * Built-in JavaScript methods: `Math.max()` and `Array.prototype.map()` * Custom implementation of `map` and `max` functions However, using a library like Lodash can simplify the code and provide a more consistent API for common tasks.
Related benchmarks:
Array.prototype.map vs Lodash.map on large data
Test map
lodash range vs Array.from vs keys() + spread + fill
native map vs lodash map on large array
Comments
Confirm delete:
Do you really want to delete benchmark?