Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare array mapping
(version: 0)
Comparing performance of:
native map vs lodash map vs native forEach push vs lodash forEach push
Created:
5 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 itemsArray = []; for (let i = 0; i < 100000; i++) { itemsArray.push({ id: i, author: 'Robert' + i, }) }
Tests:
native map
console.log(itemsArray.map(item => item.id)) console.log(itemsArray.map(item => item.author))
lodash map
console.log(_.map(itemsArray, item => item.id)) console.log(_.map(itemsArray, item => item.author))
native forEach push
let a = []; let b = []; itemsArray.forEach(item => { a.push(item.id); b.push(item.author)}) console.log(a) console.log(b)
lodash forEach push
let a = []; let b = []; _.forEach(itemsArray, item => { a.push(item.id); b.push(item.author)}) console.log(a) console.log(b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
native map
lodash map
native forEach push
lodash forEach push
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 provided benchmarking scenario. **Benchmark Definition JSON** The provided JSON defines a benchmark that compares three approaches to map an array of objects in JavaScript: 1. **Native `map()`**: Uses the built-in `map()` method on the `itemsArray` variable to create two new arrays, one for each property (`id` and `author`) of the object. 2. **Lodash `.map()`**: Uses the `_` (Lodash) library's `map()` function to achieve the same result as the native `map()` method. 3. **Native `forEach()` with push**: Uses a traditional `forEach()` loop on the `itemsArray` variable to iterate over each element and push the desired property values into two separate arrays. **Options Compared** The benchmark compares two native JavaScript approaches (`native map` and `native forEach push`) against their Lodash counterparts (`lodash map` and `lodash forEach push`). The goal is to evaluate which approach performs better in terms of speed and execution frequency per second. **Pros and Cons of Each Approach:** 1. **Native `map()`**: * Pros: Built-in, efficient, and well-maintained. * Cons: May not be as flexible or customizable as other approaches. 2. **Lodash `.map()`**: * Pros: More flexible and customizable than native `map()`, with additional utility functions available in Lodash. * Cons: Introduces an external dependency (Lodash) and may incur a small overhead due to the function call. 3. **Native `forEach()` with push**: * Pros: Can be more flexible and customizable than `map()`, as it allows for arbitrary iteration logic. * Cons: May be slower and less efficient than `map()` due to the additional overhead of using `forEach()`. **Lodash Library** The Lodash library is a popular JavaScript utility library that provides a wide range of functional programming helpers, including `map()`. It's designed to make common tasks easier and more expressive, but it also introduces an external dependency. **Special JS Feature/Syntax: None** None of the provided benchmarking code uses any special or advanced JavaScript features/syntax. **Other Alternatives** If you were looking for alternative approaches to map arrays in JavaScript, some other options might include: * Using `filter()` followed by `map()` * Using `reduce()` with an accumulator function * Utilizing modern array methods like `flatMap()` (introduced in ECMAScript 2019) However, the provided benchmarking scenario focuses on comparing native JavaScript approaches (`map()` and `forEach()`) against their Lodash counterparts, making it a good starting point for evaluating the performance trade-offs of these different methods.
Related benchmarks:
es6 vs lodash - medium data
es6 vs lodash - large data
lodash vs es6 in map method
_.map vs. array.map for object
Comparing lodash's times with Array.from
Comments
Confirm delete:
Do you really want to delete benchmark?