Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash map vs js map
(version: 0)
Comparing performance of:
JS map vs lodash map vs js reduce vs lodash reduce
Created:
4 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:
const j = [1, 4, 9, 16, 25, 35, 49, 68, 74, 86, 93, 110, 121, 135, 141, 156, 157, 158, 161, 162, 163, 164, 165, 166, 167, 168] function mapper(el) { return el + 10 } function lodashMap () { arr = _.map(j, mapper) console.log(arr) } function jsMap () { arr = j.map(mapper) console.log(arr) } function reducer (sum, el){ return sum + el } function lodashReduce () { sum = _.reduce(j, reducer, 0) console.log(sum) } function jsReduce () { sum = j.reduce(reducer, 0) console.log(sum) }
Tests:
JS map
jsMap()
lodash map
lodashMap()
js reduce
jsReduce()
lodash reduce
lodashReduce()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
JS map
lodash map
js reduce
lodash reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JS map
143603.6 Ops/sec
lodash map
43868.8 Ops/sec
js reduce
178646.2 Ops/sec
lodash reduce
170409.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of this benchmark. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case on the MeasureThat.net website. The test measures the performance difference between two approaches: using Lodash's `map()` and `reduce()` functions, versus implementing these functions from scratch (using only built-in JavaScript methods). **Approaches Compared** There are four individual test cases: 1. **`jsMap()`**: This function uses the built-in `Array.prototype.map()` method to create a new array with the mapped values. 2. **`lodashMap()`**: This function uses Lodash's `map()` function to achieve the same result as `jsMap()`. 3. **`jsReduce()`**: This function uses the built-in `Array.prototype.reduce()` method to calculate the sum of the array elements. 4. **`lodashReduce()`**: This function uses Lodash's `reduce()` function to achieve the same result as `jsReduce()`. **Pros and Cons** Here are some pros and cons of each approach: * **Built-in methods (`jsMap()` and `jsReduce()`)**: + Pros: Easy to implement, well-documented, and optimized for performance. + Cons: May not be suitable for all use cases (e.g., legacy code or specific requirements). * **Lodash's `map()` and `reduce()` functions (`lodashMap()` and `lodashReduce()`)**: + Pros: Convenient, readable, and often faster than implementing from scratch due to Lodash's optimization. + Cons: Adds an external dependency (Lodash), which may not be desirable for small projects or those with strict dependencies. **Library and Syntax Considerations** * **Lodash**: A popular JavaScript utility library that provides a wide range of functions, including `map()` and `reduce()`. Using Lodash can simplify code and improve performance. * **Special JS Features/Syntax**: The benchmark does not mention any special JavaScript features or syntax. However, some notable mentions include: + `arrow functions` (e.g., `mapper(el) => el + 10`), which are a concise way to define small functions. + `template literals` (e.g., `const j = [1, 4, 9, ...]`, where the string is expanded into an array), which can improve code readability. **Other Alternatives** If you prefer not to use built-in methods or Lodash's `map()` and `reduce()` functions, you can implement these functions from scratch using standard JavaScript loops: ```javascript // jsMap() function mapper(el) { let result = []; for (let i = 0; i < el.length; i++) { result.push(el[i] + 10); } return result; } // jsReduce() function reducer(sum, el) { return sum + el; } ``` Keep in mind that implementing these functions from scratch can be less readable and may not be as efficient as using built-in methods or Lodash's optimized implementations. I hope this explanation helps you understand the benchmark!
Related benchmarks:
Array.prototype.map vs Lodash.map
lodash map vs es6 map v2
Moz lodash map vs es6 map
lodash map vs es6 Object.values.map
Comments
Confirm delete:
Do you really want to delete benchmark?