Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash MapValue vs Pure JS [2.0]
(version: 0)
Comparing performance of:
Lodash Test vs Pure JS Test
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Script Preparation code:
function format_lodash(data) { return data.map(({ id, label, ...properties}) => { return { id, label, properties: _.mapValues(properties, v => v[0]) }; }); } function format_pure(data) { return data.map(({ id, label, ...properties}) => { for (let [key, value] of Object.entries(properties)) { properties[key] = value[0] } return { id, label, properties}; }); }
Tests:
Lodash Test
const input = [{ id: 1, label: 'label 1', property_1: ['prop_1'], property_2: ['prop_2'] }, { id: 2, label: 'label 2', property_3: ['prop_3'], property_4: ['prop_4'] } ] console.log(format_lodash(input))
Pure JS Test
const input = [{ id: 1, label: 'label 1', property_1: ['prop_1'], property_2: ['prop_2'] }, { id: 2, label: 'label 2', property_3: ['prop_3'], property_4: ['prop_4'] } ] console.log(format_pure(input))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash Test
Pure JS Test
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 benchmark and its components. **Benchmark Definition** The benchmark definition consists of two functions: `format_lodash` and `format_pure`. These functions are used to format an input array of objects, which is expected to have an "id" property, a "label" property, and one or more additional properties with array values. The goal of the benchmark is to compare the performance of these two approaches: 1. **Lodash**: The `format_lodash` function uses the Lodash library (version 4.17.15) to map over the object's properties using `_mapValues`. This approach assumes that the array values are always arrays of length 1. 2. **Pure JS**: The `format_pure` function iterates over the object's properties using `Object.entries`, and then updates each property value to its first element in the array. **Options Compared** The two approaches differ in how they process the array values: * Lodash uses `_mapValues` to perform a "partial application" of the map function, which reduces the array value to a single value. This approach assumes that the array value is always an array of length 1. * Pure JS iterates over each property and updates its value to its first element in the array. **Pros and Cons** Here are some pros and cons for each approach: * **Lodash**: + Pros: Simple, concise code, and Lodash provides a utility function that performs the desired operation. + Cons: Assumes a specific format of array values, which may not always be the case. Additionally, Lodash is an external dependency that needs to be included in the benchmark process. * **Pure JS**: + Pros: No assumptions about array formats, and only uses built-in JavaScript functions, making it more self-contained. + Cons: More verbose code compared to the Lodash approach. **Library Used** The `format_lodash` function relies on the Lodash library (version 4.17.15), which provides utility functions for working with arrays and objects. **Special JS Feature or Syntax** There are no specific JavaScript features or syntaxes used in these benchmark definitions. However, it's worth noting that the use of `_mapValues` in the Lodash approach may be considered a "niche" feature, but it's still a standard part of the Lodash library. **Other Alternatives** If you wanted to create an alternative benchmark, you could explore other approaches, such as: * Using a different mapping function (e.g., `Array.prototype.map` instead of `_mapValues`) * Adding additional properties or complexity to the input data * Comparing performance with different array data structures (e.g., objects instead of arrays) * Experimenting with different optimizations, such as caching or memoization Keep in mind that the goal of a benchmark is to compare the relative performance of two approaches under specific conditions. The specific alternatives you choose will depend on your goals and requirements for the benchmark.
Related benchmarks:
Object.keys vs lodash _.keys
lodash (v4.17.15) map vs Object.keys map
Lodash MapValue vs Pure JS
array.map vs _.map
Comments
Confirm delete:
Do you really want to delete benchmark?