<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
const stats = { size: 5, age: 37, length: 10 };
const result = Object.fromEntries(
Object.entries(stats).map(([key, value]) => [key, value * 3])
);
const stats = { size: 5, age: 37, length: 10 };
const result = _.mapValues(stats, (val) => val * 3);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.entries and Array.map | |
Lodash map |
Test name | Executions per second |
---|---|
Object.entries and Array.map | 450310.7 Ops/sec |
Lodash map | 1135384.8 Ops/sec |
Let's dive into the JavaScript microbenchmarking world of MeasureThat.net.
Overview
The benchmark provided compares two approaches to perform object mapping: using Lodash's mapValues
method versus using native Object.entries
and Array.map
. The test is designed to measure the performance difference between these two methods in a JavaScript engine.
Options Compared
Two options are compared:
mapValues
method: This method is part of the Lodash library, which provides a utility function for mapping values in an object.Object.entries
and Array.map
combination: This approach uses the Object.entries
method to extract key-value pairs from an object, maps over the resulting array using Array.map
, and then creates a new object with the transformed key-value pairs.Pros and Cons
Lodash's mapValues
method:
Pros:
Cons:
Native Object.entries
and Array.map
combination:
Pros:
Cons:
Library Used
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object transformation, and more. In this benchmark, Lodash's mapValues
method is used to perform the object mapping.
Special JS Feature or Syntax
None are mentioned in the provided code snippets, so we can assume they are standard JavaScript features.
Other Alternatives
If you're looking for alternative libraries or approaches for performing object mapping, consider:
Object.keys()
, Array.prototype.forEach()
, and other basic JavaScript techniques.For the specific problem of comparing object mapping performance, you may also consider using a benchmarking library like Benchmark.js or Microbenchmark to create more comprehensive benchmarks.