Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Times vs Array.from callback
(version: 0)
_times(n,()=>func()) vs. Array.from({length: n},(_,i) => func())
Comparing performance of:
lodash times vs Array.from
Created:
4 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 nTimes = 1000 var func = (i) => i * Math.random()
Tests:
lodash times
_.times(nTimes,func)
Array.from
Array.from({length: nTimes},(_,i) => func(i))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash times
Array.from
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 benchmark and its results. **What is being tested?** The benchmark tests two approaches to iterate `nTimes` number of times, where each iteration calls a function `func`. The two approaches are: 1. Using Lodash's `_times` function 2. Using `Array.from` with a callback function **Options compared:** * **Lodash's `_times` function**: This is a utility function that takes three arguments: the number of times to iterate (`nTimes`), and a callback function (`func`). It returns an array where each element is the result of calling `func` in each iteration. * **`Array.from` with a callback function**: This method creates a new array from an existing array-like object (in this case, an empty object) by repeatedly applying a callback function to create new elements. **Pros and Cons:** * **Lodash's `_times` function**: + Pros: - Easy to use and understand - Provides a clear and concise way to iterate `nTimes` + Cons: - Requires the Lodash library, which may add overhead - May not be as performant as native methods (see below) * **`Array.from` with a callback function**: + Pros: - Native method, so no additional library is required - Can be more performant than using `_times` + Cons: - Requires manual memory management for the array-like object - May be less readable if not used carefully **Library and syntax:** * **Lodash's `_times` function**: This function uses Lodash's internal implementation, which is optimized for performance. The callback function passed to `_times` is called in each iteration, returning a value that is collected into an array. * **`Array.from` with a callback function**: This method uses the `Array.prototype.forEach` method internally, which calls the provided callback function on each element of the array-like object. **Special JS feature or syntax:** None mentioned in this benchmark. **Other alternatives:** If you need to iterate over a large number of times, you could also consider using: * **For...of loop**: A more modern and concise way to iterate over a range of numbers. * **`reduce()` method**: While not directly applicable here, `reduce()` can be used in conjunction with `_times` or `Array.from` to perform more complex operations. Keep in mind that the choice of approach depends on your specific use case and performance requirements.
Related benchmarks:
native slice vs lodash slice
Unique lodash vs vanilla
lodash times vs Array.from
Array.prototype.every vs Lodash every()
Comments
Confirm delete:
Do you really want to delete benchmark?