Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash range vs Array.from vs keys() + spread
(version: 0)
Comparing performance of:
lodash range vs array from vs keys spread
Created:
6 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({length: 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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash range
21825.4 Ops/sec
array from
9751.8 Ops/sec
keys spread
10099.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. **Overview** The JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The test compares the performance of three different approaches to generate an array of numbers: 1. `lodash.range(size)` 2. `Array.from({length: size}, (_, i) => i)` 3. `[...Array(size).keys()]` **Lodash library** `_.range(size)` is a function from the Lodash library, which provides utility functions for functional programming. The purpose of this function is to generate an array of numbers from 0 to `size-1`. **Spread syntax** The second approach uses the spread syntax `[...Array(size).keys()]`. This syntax creates a new array by spreading the keys of an existing array (`Array(size)`). The `Array(size)` part generates an array with `size` elements, and the `.keys()` method returns an array of numbers from 0 to `size-1`. **Pros and Cons** * **Lodash's `_range` function:** + Pros: Simple and concise, well-tested, and widely used. + Cons: Requires including additional library code (Lodash), which may impact page load times or affect performance in other ways. * **Spread syntax `[...Array(size).keys()]`:** + Pros: Fast and lightweight, as it only uses built-in JavaScript methods. + Cons: May be less readable for some developers, especially those not familiar with the spread syntax. **Benchmarking** The benchmark measures the execution speed of each approach by executing a loop `size` times using each method. The results are reported in the format of: * `ExecutionsPerSecond`: The number of executions performed per second. * `TestName`: The name of the test case (e.g., "keys spread"). **Other alternatives** If you're interested in exploring other approaches, here are a few examples: 1. **`Array.from()` without spread syntax:** Use `Array.from(Array(size), (_, i) => i)` to generate an array with numbers from 0 to `size-1`. This approach is similar to the second one but avoids the spread syntax. 2. **Manual indexing:** Implement your own loop using manual indexing (e.g., `var result = []; for (var i = 0; i < size; i++) { result.push(i); }`) to generate an array of numbers. Keep in mind that the performance differences between these approaches might be negligible, and other factors like hardware, JavaScript engine, or specific use cases may impact the results.
Related benchmarks:
_.range(N) vs [...Array(N).keys()]
lodash range vs Array.from vs keys() + spread + fill
lodash range vs Array.from + new Array vs keys() + spread
lodash range vs Array.from vs keys() + spread 234das
Comments
Confirm delete:
Do you really want to delete benchmark?