<!--your preparation HTML code goes here-->
var a1 = ["fox", "drawing", "art", "animal", "posture", "fur", "sketch", "creativity", "playful", "standing", "wildlife", "illustration", "detail", "nature", "creative expression", "artist", "unique", "tail", "stance", "animal behavior", "whimsical", "illustration style", "fine art", "drawing techniques", "imaginative", "hand-drawn"];
var a2 = ["dance", "celebration", "lights", "fox", "drawing", "art", "colors", "whimsical", "floor", "party", "evening", "creative expression", "artist", "unique", "tail", "stance", "shadows", "entertainment", "people", "socialize", "energy", "rhythm", "colorful", "bright", "texture", "ambiance", "nightlife", "mood", "design", "pattern", "event space", "nightlife scene", "sound", "celebration area"];
const uniqA = a1.filter((x) => !a2.includes(x))
const uniqA2 = [];
a1.map((x) => !a2.includes(x) ? uniqA2.push(x) : x)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
.filter() with .includes() | |
.map() with .includes() |
Test name | Executions per second |
---|---|
.filter() with .includes() | 911931.2 Ops/sec |
.map() | 996270.4 Ops/sec |
The benchmark provided tests two approaches for removing duplicate items from one array (a1
) that are present in another array (a2
). The performance of these two methods is compared in terms of execution speed.
Using .filter()
with .includes()
:
const uniqA = a1.filter((x) => !a2.includes(x));
Description: This approach filters through a1
, retaining only the elements that are not found in a2
by checking each element of a1
against all elements of a2
using the .includes()
method.
Pros:
Cons:
.includes()
performs a linear search for each element in a1
, leading to a time complexity of O(n*m), where n is the length of a1
and m is the length of a2
.Using .map()
with manual pushing:
const uniqA2 = [];
a1.map((x) => !a2.includes(x) ? uniqA2.push(x) : x);
Description: In this method, a1
is iterated through using .map()
, where for each element, it checks if the element exists in a2
using .includes()
. If it doesn't exist, the element is pushed to a new array uniqA2
.
Pros:
Cons:
.includes()
.uniqA2
) might introduce overhead, as it may require more memory.From the benchmark results, it's noted that the .map()
approach performed better than the .filter()
combined with .includes()
, with an execution speed of approximately 996,270.43 executions per second versus 911,931.19 for the latter.
While the methods tested are valid, there are alternative approaches that might yield better performance, especially for larger datasets:
Using a Set:
Set
to hold the elements of a2
, membership tests can be performed in constant time:const setA2 = new Set(a2);
const uniqA = a1.filter(x => !setA2.has(x));
Using a HashMap:
The benchmark effectively demonstrates the performance differences between common JavaScript methods for handling array manipulations. While the tested methods are accessible for many developers, exploring alternatives such as Sets for deduplication tasks can significantly enhance performance and scalability in real-world applications, making code cleaner and faster, especially when dealing with large datasets.