<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = ['a', 'b', 'c']
array.map((value, index) => { console.log(value) })
_.map(array, (value, index) => { console.log(value) })
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native map | |
_.map |
Test name | Executions per second |
---|---|
Native map | 40916.3 Ops/sec |
_.map | 45103.6 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of two approaches to iterate over an array: using JavaScript's native map()
function versus using the popular utility library Lodash's _map()
function.
Options Compared
Two options are compared:
map()
: This is a built-in JavaScript function that applies a transformation to each element in an array._map()
: This is a part of the Lodash library, which provides a functional programming style for working with arrays.Pros and Cons
map()
:_map()
:Library - Lodash
Lodash is a popular utility library that provides a wide range of functions for working with arrays, objects, and more. The _map()
function is one of the most commonly used functions in Lodash, allowing you to transform each element in an array.
Special JS Feature/Syntax
The benchmark uses ES6 syntax, specifically arrow functions (=>
) and template literals (<script src='https://...'
). This is a relatively modern JavaScript feature that was introduced in ECMAScript 2015 (ES6).
Other Alternatives
If you're interested in testing other approaches to iterating over arrays, some alternatives could include:
forEach()
or for
loopsHowever, these alternatives might not be as relevant to this specific benchmark, which is focused on comparing native JavaScript's map()
function versus Lodash's _map()
function.
Overall, this benchmark provides a simple and practical way to compare the performance of two different approaches to iterating over arrays in JavaScript.