Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native map vs _.map
(version: 3)
Comparing performance of:
native map vs _.map vs myMap (1st variation) vs myMap (2nd variation) vs myMap (3rd variation)
Created:
5 years ago
by:
Registered User
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 arr = ['a', 'b', 'c'];
Tests:
native map
arr.map(v=>v+'hi')
_.map
_.map(arr,v=>v+'hi')
myMap (1st variation)
function myMap(arr,func) { if(!Array.isArray(arr)) return -1; var res=[]; for(let i=0;i<arr.length;i++) res.push(func(arr[i],i,arr)) return res; } myMap(arr,v=>v+'hi')
myMap (2nd variation)
var is_array = function (value) {return value && typeof value === 'object' && typeof value.length === 'number' && typeof value.splice === 'function' && !(value.propertyIsEnumerable('length'));}; function myMap(arr,func) { if(!is_array(arr)) return -1; var res=Array(arr.length); for(let i=0;i<arr.length;i++) res[i]=func(arr[i],i,arr) return res; } myMap(arr,v=>v+'hi')
myMap (3rd variation)
var is_array = function (value) {return value && typeof value === 'object' && typeof value.length === 'number' && typeof value.splice === 'function' && !(value.propertyIsEnumerable('length'));}; function myMap(arr,func) { if(!is_array(arr)) return -1; return arr.map(func); } myMap(arr,v=>v+'hi')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
native map
_.map
myMap (1st variation)
myMap (2nd variation)
myMap (3rd variation)
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 dive into the benchmark and explore what's being tested. **Overview** The benchmark compares the performance of three different approaches to create a new array by mapping over an existing one: 1. Native `map()` function 2. Lodash's `_map()` function 3. Custom `myMap()` functions with variations **What's being compared?** Here are the specific differences between each approach: * **Native `map()`**: The built-in JavaScript method that takes a callback function as an argument and returns a new array with the results of applying that callback to each element in the original array. * **Lodash's `_map()`**: A utility function from the Lodash library that performs the same operation as the native `map()` function. It's optimized for performance and provides additional features like memoization and support for iterating over arrays in reverse order. * **Custom `myMap()` functions**: Three variations of a custom implementation, each with its own approach to handling array validation, iteration, and result processing. **Pros and Cons** Here are some pros and cons for each approach: * **Native `map()`**: + Pros: Built-in JavaScript method, easy to use, and optimized for performance. + Cons: May not be as efficient as custom implementations or libraries like Lodash. * **Lodash's `_map()`**: + Pros: Optimized for performance, provides additional features like memoization, and supports reverse iteration. + Cons: Requires including the Lodash library, which adds overhead. * **Custom `myMap()` functions**: + Pros: Can be optimized for specific use cases or environments, allows for fine-grained control over array validation and result processing. + Cons: More complex to implement, requires manual error handling, and may not be as performant as native methods. **Library usage** The Lodash library is used in this benchmark to provide an alternative implementation of the `map()` function. The `_map()` function is used in two test cases, while a custom implementation is used in three other variations. **Special JS features or syntax** There are no special JavaScript features or syntaxes mentioned in this benchmark. **Alternatives** If you're interested in exploring alternatives to these approaches, here are some options: * Use the built-in `map()` function, as it's widely supported and optimized for performance. * Consider using libraries like Lodash or Ramda, which provide high-performance implementations of functional programming utilities. * Implement your own custom `myMap()` functions for specific use cases or environments. Keep in mind that each approach has its trade-offs, and the best choice depends on the specific requirements and constraints of your project.
Related benchmarks:
lodash map vs. vanilla map
native map vs lodash _.map
Native map vs Lodash map
array.map vs _.map
Array Map Vs Lodash Map (1)
Comments
Confirm delete:
Do you really want to delete benchmark?