const nonUnique = ['one', 'two', 'three', 'three', 'four', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const unique = Object.keys(nonUnique.reduce((acc, item) => {
if (!acc[item]) {
acc[item] = true
};
return acc;
}, {})).keys();
const nonUnique = ['one', 'two', 'three', 'three', 'four', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const unique = Array.from(new Set(nonUnique));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using an object | |
Using a Set |
Test name | Executions per second |
---|---|
Using an object | 3975661.8 Ops/sec |
Using a Set | 3080596.2 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents two benchmark definitions, which test the performance of creating a unique array of strings from a possible non-unique array.
Benchmark Definitions
The first benchmark definition uses an object:
const nonUnique = ['one', 'two', 'three', 'three', 'four', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const unique = Object.keys(nonUnique.reduce((acc, item) => {
if (!acc[item]) {
acc[item] = true;
}
return acc;
}, {})).keys();
The second benchmark definition uses a Set:
const nonUnique = ['one', 'two', 'three', 'three', 'four', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const unique = Array.from(new Set(nonUnique));
Comparison of Approaches
Object.keys()
method, which is a built-in JavaScript function that returns an array of strings representing the keys of an object. It's a concise and readable way to create a unique array of strings.reduce()
method can be slower than other approaches because it iterates over the array multiple times. Additionally, this approach requires more memory to store the intermediate result (the acc
object).Array.from()
method can be slower than other approaches because it creates an array from the Set's values, which may not be necessary in this case. Additionally, Sets are inherently unordered data structures, so if you need to preserve a specific order, this approach may not work.Other Considerations
Both approaches have their trade-offs in terms of performance, readability, and memory usage. The choice between them ultimately depends on the specific requirements of your use case.
If speed is crucial and readability is not a concern, using a Set might be the better choice. However, if conciseness and ease of understanding are more important than raw performance, using an object might be the way to go.
Libraries
Neither benchmark definition uses a library in its provided code. However, both approaches utilize built-in JavaScript functions (Object.keys()
and Array.from()
) that provide efficient set operations.
If you needed to perform similar set-related operations in your own code, you could consider using libraries like Lodash or Set.js, which provide additional functionality for working with Sets and other data structures.
Special JS Features
Neither benchmark definition uses any special JavaScript features or syntax. The provided code is straightforward and easy to understand, making it accessible to a wide range of software engineers without deep knowledge of JavaScript.
If you'd like to explore more advanced JavaScript features, MeasureThat.net provides an extensive collection of benchmark definitions that cover various aspects of the language, including performance-critical topics like closures, async/await, and more.