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 = 5; 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):
I'll break down the benchmark and its test cases for you. **Benchmark Definition** The benchmark uses JavaScript to measure performance of three different operations: `map`, `reduce`, and `filter`. The operations are applied to two arrays: `all` and `ids`. * `all` contains 1000 objects with a unique identifier (`id`) and a value (`value`). * `ids` is an array of integers representing the indices of the `id`s in the `all` array. * The operations are applied to these arrays, and the results are logged to the console. **Benchmark Preparation Code** The script preparation code sets up the benchmark by: 1. Generating a random shuffle for both `all` and `ids` arrays using the `shuffle()` function. 2. Defining two constants: `allLength` (1000) and `idsLength` (5). 3. Creating the `all` and `ids` arrays with their respective values. **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library, version 2.4.1. **Test Cases** There are three test cases: * **map + find iterate on ids**: This test case applies `map()` and `find()` functions to get the values of all objects in `all` array whose `id` matches with any element in `ids`. * **reduce + includes iterate on all**: This test case applies `reduce()` function along with `includes()` function to filter elements from `all` array based on their presence in `ids`. * **filter + includes + map iterate on all**: This test case combines the above two operations, first filtering elements of `all` using `filter()`, then mapping over that filtered set and applying `map()` on it. **Options Compared** The benchmark compares the performance of three different approaches for each operation: 1. **Map + Find Iterate on IDs**: Uses `map()` followed by `find()` to find the desired elements. 2. **Reduce + Includes Iterate on All**: Utilizes `reduce()` function with `includes()` helper function to achieve similar results. 3. **Filter + Includes + Map Iterate on All**: Applies both `filter()` and `map()` operations sequentially. **Pros and Cons** Each approach has its advantages: * **Map + Find Iterate on IDs**: Provides a straightforward implementation but may require more iterations (e.g., finding the index of an id in ids). * **Reduce + Includes Iterate on All**: More efficient because it avoids unnecessary lookups by directly including elements from all array into a result. However, requires extra iteration. * **Filter + Includes + Map Iterate on All**: Offers the best balance between readability and performance since it leverages both filtering (to eliminate unwanted results) followed by mapping. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a comprehensive set of functions for functional programming, including `map()`, `reduce()`, `filter()`, `includes()` etc. It helps simplify the code and reduces its readability. **Special JS Feature/Syntax** This benchmark does not explicitly use any special JavaScript features or syntax other than using ES6 imports with CommonJS, but it relies heavily on functional programming (map/reduce/filter) to perform operations.
Related benchmarks:
lodash test
lodash test
lodash test
lodash shuffle
Comments
Confirm delete:
Do you really want to delete benchmark?