Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash range vs Array.from + new Array vs keys() + spread
(version: 0)
Comparing performance of:
lodash range vs array from vs keys spread
Created:
3 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 size = 10000;
Tests:
lodash range
_.range(size);
array from
Array.from(new Array(size), (_, i) => i);
keys spread
[...Array(size).keys()]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash range
array from
keys spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash range
48956.5 Ops/sec
array from
4689.9 Ops/sec
keys spread
5188.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance of three different approaches to create an array with a specified size in JavaScript: 1. **Lodash `_.range()`**: This function returns an array of numbers from 0 to the specified size minus one. 2. **`Array.from(new Array(size), (_, i) => i)`**: This method creates a new array by mapping the callback function to each element in the `new Array(size)`. 3. **`[...Array(size).keys()]`**: This syntax uses the spread operator (`...`) and the `keys()` method of an array to create a new array with the keys of the original array. **Options Compared** The three options are compared in terms of their performance, which is measured by the number of executions per second. **Pros and Cons of Each Approach** 1. **Lodash `_.range()`**: * Pros: Simple and concise syntax. * Cons: May have a higher overhead due to the use of Lodash library. 2. **`Array.from(new Array(size), (_, i) => i)`**: * Pros: Flexible and can be used with other array methods. * Cons: Requires creating an intermediate array, which may lead to additional memory allocation. 3. **`[...Array(size).keys()]`**: * Pros: Efficient and concise syntax using the spread operator and `keys()` method. * Cons: May not work in older browsers that do not support modern array methods. **Library Usage** The benchmark uses the Lodash library, which provides a simple and convenient way to create arrays with a specified size. The `_.range()` function is a part of the Lodash library. **Special JS Feature/Syntax** None mentioned in this specific benchmark. **Other Alternatives** If you need to create an array with a specified size in JavaScript, other alternatives include: * Using the `Array.from()` method with a generator function or another closure. * Using a simple loop to push elements into an array. * Using a library like underscore.js (similar to Lodash) or Ramda. Note that the choice of approach depends on the specific use case and performance requirements.
Related benchmarks:
lodash range vs Array.from vs keys() + spread
_.range(N) vs [...Array(N).keys()]
lodash range vs Array.from vs keys() + spread + fill
lodash range vs Array.from vs keys() + spread 234das
Comments
Confirm delete:
Do you really want to delete benchmark?