function generateRandomObjects(count) {
const getRandomString = (length = 5) =>
Math.random().toString(36).substring(2, 2 + length);
return Array.from({
length: count
}, () => ({
item: getRandomString(),
descr: getRandomString(),
}));
}
// Пример использования:
const randomObjects = generateRandomObjects(3000);
console.log(randomObjects);
const uniqItems = new Set();
const items = [];
for (const element of randomObjects) {
const key = `${element.item} - ${element.descr}`;
if (!uniqItems.has(key)) {
items.push(element);
uniqItems.add(key);
}
}
const items = Array.from(
new Map(
randomObjects.map(i => [`${i.item} - ${i.descr}`, { item: i.item, descr: i.descr }])
).values()
);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set and ForOf | |
Map |
Test name | Executions per second |
---|---|
Set and ForOf | 6114.2 Ops/sec |
Map | 4731.9 Ops/sec |
The benchmark provided is focused on evaluating the performance of different methods for filtering unique objects from an array in JavaScript. The test specifically examines two different approaches to achieve this: using a Set
in conjunction with a for...of
loop and using a Map
. Here's a detailed breakdown of the benchmark, the methods compared, and their pros and cons.
Set and ForOf:
const uniqItems = new Set();
const items = [];
for (const element of randomObjects) {
const key = `${element.item} - ${element.descr}`;
if (!uniqItems.has(key)) {
items.push(element);
uniqItems.add(key);
}
}
Set
to keep track of unique keys derived from the combination of the item
and descr
properties of each object. The for...of
loop iterates through randomObjects
, adding to the items
array only those elements whose keys are not already present in the Set
.Map:
const items = Array.from(
new Map(
randomObjects.map(i => [`${i.item} - ${i.descr}`, { item: i.item, descr: i.descr }])
).values()
);
Map
where each entry is created by mapping the randomObjects
array to pairs of keys (the combined item
and descr
) and their corresponding objects. The unique objects are then extracted through the values()
method of the Map
, converted back into an array with Array.from()
.Based on the execution results from the benchmark:
Set and ForOf:
Map:
map
and Array.from
functions, which can lead to clearer code for some developers who favor functional programming styles.Map
.Set
and Map
), which can impact memory usage when working with large datasets.Set
and Map
are well-supported in modern browsers, but developers should consider compatibility when targeting older environments.In addition to these two methods, there are other approaches for achieving the same functionality:
Array.prototype.filter combined with Array.prototype.indexOf
could be employed to filter unique objects, though this would typically be less efficient as it requires scanning the array for each element, leading to higher time complexity.
Using Lodash or other libraries: Libraries like Lodash provide utility functions such as _.uniqBy()
, which can simplify the code further but add external dependencies.
Using plain object as a hash map: Instead of Set
or Map
, one could use a plain object to track unique items, but this requires additional logic to ensure key uniqueness effectively.
These additional alternatives may prioritize different trade-offs between performance, readability, and external dependencies based on specific project requirements or developer preferences.