Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash test
(version: 0)
Comparing performance of:
map + find iterate on ids vs reduce + includes iterate on all vs filter + includes + map iterate on all
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
Script Preparation code:
function shuffle(a) { var j, x, i; for (i = a.length; i; i--) { j = Math.floor(Math.random() * i); x = a[i - 1]; a[i - 1] = a[j]; a[j] = x; } } var all = []; var ids = []; var allLength = 1000; var idsLength = 800; for(var i = 0; i < allLength; i++) { all[i] = {id: i, value: 'value' + i }; } for(var i = 0; i < allLength; i++) { ids[i] = i; } ids = ids.splice(allLength - idsLength) shuffle(all); shuffle(ids);
Tests:
map + find iterate on ids
var output = _.map(ids, function(id) { return _.find(all, { id: id }); }); console.log(output);
reduce + includes iterate on all
var output = _.reduce(all, function(acc, element) { if(_.includes(ids, element.id)) { acc.push(element); } return acc; }, []); console.log(output);
filter + includes + map iterate on all
var output = _.filter(all, function(element) { return _.includes(ids, element.id); }); console.log(output);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map + find iterate on ids
reduce + includes iterate on all
filter + includes + map iterate on all
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 dive into the details of this JavaScript microbenchmark. **Benchmark Definition** The provided JSON represents a benchmarking framework, where the user defines a script that showcases various operations using the Lodash library, a popular utility library for functional programming in JavaScript. The script is divided into two parts: 1. **Script Preparation Code**: This section generates an array of 1000 objects and another array of IDs, shuffles both arrays to randomize their order. These arrays are then used as input data for the benchmarking tests. 2. **Html Preparation Code**: This line includes a script tag that loads the Lodash library version 2.4.1. **Individual Test Cases** There are three test cases defined: 1. **map + find iterate on ids**: This test case uses `lodash.map()` and `lodash.find()` to iterate over the IDs array and find the corresponding element in the "all" array. 2. **reduce + includes iterate on all**: This test case uses `lodash.reduce()` and `lodash.includes()` to filter elements from the "all" array based on their presence in the IDs array. 3. **filter + includes + map iterate on all**: This test case is a combination of the previous two, using `lodash.filter()`, `lodash.includes()`, and `lodash.map()`. **Library: Lodash** Lodash is a utility library that provides a wide range of functions for functional programming in JavaScript. The library includes operations such as: * Iteration (e.g., `map()`, `filter()`) * Array manipulation (e.g., `reduce()`, `includes()`) * String manipulation (e.g., `find()`) **Special JS Features/Syntax** None are explicitly mentioned in the benchmark definition. **Options Compared** The three test cases differ in the order and combination of operations used: 1. **map + find**: This approach iterates over the IDs array using `map()`, finds the corresponding element in the "all" array using `find()`, and then logs the output. 2. **reduce + includes**: This approach uses `reduce()` to filter elements from the "all" array based on their presence in the IDs array, which is faster because it avoids unnecessary iterations over the entire array. 3. **filter + includes + map**: This approach combines the filtering and mapping operations, which can be slower than using separate functions like `reduce()`. **Pros and Cons** * **map + find**: Pros: easy to understand and implement. Cons: may be slow due to repeated iterations over the same data structures. * **reduce + includes**: Pros: faster because it avoids unnecessary iterations over large arrays. Cons: may require more complex code to understand and implement. * **filter + includes + map**: Pros: combines filtering and mapping operations, which can be useful in certain scenarios. Cons: may be slower than using separate functions. **Other Alternatives** If you were to rewrite these benchmarks without using Lodash, you might consider alternative libraries or built-in JavaScript functions that achieve similar results. For example: * `Array.prototype.filter()`, `Array.prototype.indexOf()` * `for` loops and manual iteration over arrays * Other utility libraries like Underscore.js or Ramda Keep in mind that these alternatives may not provide the same level of convenience, readability, and performance as Lodash. I hope this explanation helps!
Related benchmarks:
lodash test
lodash test
lodash test
lodash shuffle
Comments
Confirm delete:
Do you really want to delete benchmark?