<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var size = 10000;
_.range(size);
Array.from({length: size}, (_, i) => i);
[Array(size).keys()]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash range | |
array from | |
keys spread |
Test name | Executions per second |
---|---|
lodash range | 21825.4 Ops/sec |
array from | 9751.8 Ops/sec |
keys spread | 10099.7 Ops/sec |
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:
lodash.range(size)
Array.from({length: size}, (_, i) => i)
[...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
_range
function:[...Array(size).keys()]
: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:
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.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.