Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash times vs new array fill vs array.from
(version: 1)
new default array
Comparing performance of:
lodash times vs new Array().fill() vs Array.from
Created:
one year 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 size = 1000000; var defaultValue = 10;
Tests:
lodash times
_.times(size, defaultValue);
new Array().fill()
new Array(size).fill(defaultValue)
Array.from
Array.from({length: size}, () => defaultValue);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash times
new Array().fill()
Array.from
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash times
10.4 Ops/sec
new Array().fill()
190.9 Ops/sec
Array.from
78.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its results. **Benchmark Overview** The benchmark measures the performance of three different approaches for filling an array with a default value in JavaScript: 1. Using `lodash.times()` 2. Creating a new array using the `new Array()` constructor and the `.fill()` method 3. Using `Array.from()` to create an array from an object **Options Compared** The benchmark compares the performance of these three approaches on a dataset of 1,000,000 elements with a default value of 10. * `lodash.times()`: This function uses a loop to repeat a block of code `size` number of times and assigns the result to an array. * `new Array().fill(defaultValue)`: This approach creates a new array with a fixed length (`size`) and fills it with the default value using the `.fill()` method. * `Array.from({length: size}, () => defaultValue)`: This approach uses the `Array.from()` method to create an array from an object, where each iteration of the object's `for...of` loop generates an element in the new array. **Pros and Cons** Here are some pros and cons for each approach: * `lodash.times()`: + Pros: Easy to read and write, simple syntax. + Cons: May be slower due to the overhead of the loop. * `new Array().fill(defaultValue)`: + Pros: Fast and efficient, creates a new array with a fixed length. + Cons: Requires a static value for the default value (cannot use a dynamic expression). * `Array.from()`: + Pros: Modern syntax, easy to read and write. + Cons: May be slower due to the overhead of creating an object. **Library Used** The `lodash.times()` function is part of the popular JavaScript utility library Lodash. The purpose of this library is to provide a set of helper functions for common tasks, such as array manipulation, string manipulation, and more. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in these benchmarks. **Other Alternatives** If you're interested in exploring alternative approaches, here are some other options: * Using `Array.prototype.fill()` (ES6+): This method is similar to the `.fill()` method on arrays, but can be called directly on the array prototype. * Using a custom implementation: You could write your own implementation of the three approaches using JavaScript's built-in methods and data structures. Keep in mind that these alternative approaches may not be as efficient or readable as the ones tested in the benchmark.
Related benchmarks:
lodash times vs Array.from
Length vs Lodash Size 100k
Spread Operator vs Lodash [2]
Array From vs lodash clone
Comments
Confirm delete:
Do you really want to delete benchmark?