Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new set vs find
(version: 0)
Comparing performance of:
Reduce vs Map
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Reduce
const data = [{ billedBy: 'Paul' }, { billedBy: 'Ola' }, { billedBy: 'Miyo' }, { billedBy: 'Ola' }, { billedBy: 'Ola' }, { billedBy: 'Mau' }, { billedBy: 'Marco' }, { billedBy: 'Mau' }, { billedBy: 'Mau' }, { billedBy: 'Marco' }, { billedBy: 'Marco' }, { billedBy: 'Marco' }, ] const companyNames = data ? data.reduce((a, c) => { if (!a.find(({ value }) => value === c.billedBy)) { a.push({ name: c.billedBy, value: c.billedBy }); } return a; }, []) : [];
Map
const data = [{ billedBy: 'Paul' }, { billedBy: 'Ola' }, { billedBy: 'Miyo' }, { billedBy: 'Ola' }, { billedBy: 'Ola' }, { billedBy: 'Mau' }, { billedBy: 'Marco' }, { billedBy: 'Mau' }, { billedBy: 'Mau' }, { billedBy: 'Marco' }, { billedBy: 'Marco' }, { billedBy: 'Marco' }, ] const companyNames = data ? Array.from(new Set(data.map(({ billedBy }) => billedBy))).map( (billedBy) => ({ name: billedBy, value: billedBy, }) ) : [];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
Map
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 break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark definition is a JSON object that contains information about the benchmark to be executed. It has four properties: * `Name`: The name of the benchmark, which in this case is "new set vs find". * `Description`: An optional description of the benchmark. * `Script Preparation Code`: An optional code snippet that can be executed before running the benchmark. * `Html Preparation Code`: An optional HTML code snippet that can be executed before running the benchmark. In this case, both properties are empty, indicating that no preparation code is required. **Individual Test Cases** There are two test cases: 1. **Reduce** The test case uses the `reduce()` method to create a new set from an array and then convert it back to an array using the `map()` method. 2. **Map** The test case uses the `map()` function to create a new array from an array, filtering out duplicates by converting it to a set. **Options Compared** In this benchmark, two different approaches are being compared: 1. **Reduce**: Using the `reduce()` method to create a new set and then convert it back to an array. 2. **Map**: Using the `map()` function to create a new array from an array, filtering out duplicates by converting it to a set. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: 1. **Reduce** Pros: * Can be more efficient for large datasets since it avoids creating intermediate arrays. * Can be more concise than using `map()`. Cons: * Requires the use of the `reduce()` method, which may not be familiar to all developers. * May have performance issues if the array is very large. 2. **Map** Pros: * More concise and readable than using `reduce()`. * Easier to understand for developers who are familiar with `map()`. Cons: * Creates intermediate arrays, which can increase memory usage. * May be less efficient than `reduce()` for very large datasets. **Library Used** In both test cases, the `Set` object is used. A set in JavaScript is a collection of unique values, and it's implemented as an unordered map. The `Set` object provides a fast way to add or remove elements from the collection without checking for duplicates. **Special JS Feature/ Syntax** No special JavaScript feature or syntax is used in this benchmark. However, it's worth noting that the use of `const` keywords to declare variables is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Other Alternatives** If you were to rewrite these benchmarks using alternative approaches, here are some options: * Instead of using `Set`, you could use an array with duplicate values and then remove duplicates manually. * You could use the `filter()` method instead of `map()` to create a new array with duplicates removed. * You could use a library like Lodash's `uniq` function to remove duplicates from an array. Keep in mind that these alternatives may have different performance characteristics or code readability implications compared to using the `Set` object or `map()`.
Related benchmarks:
Array.prototype.find vs Lodash find 2
has vs find
array first vs find
set.has vs. array.find
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?