Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript unique string array with array-valued return type
(version: 0)
Create a unique array of strings from a possible non-unique array.
Comparing performance of:
Using an object vs Using a Set
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Using 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();
Using a Set
const nonUnique = ['one', 'two', 'three', 'three', 'four', 'four', 'five', 'six', 'seven', 'eight', 'nine']; const unique = Array.from(new Set(nonUnique));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using an object
Using a Set
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):
**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: ```javascript 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: ```javascript const nonUnique = ['one', 'two', 'three', 'three', 'four', 'four', 'five', 'six', 'seven', 'eight', 'nine']; const unique = Array.from(new Set(nonUnique)); ``` **Comparison of Approaches** 1. **Using an object**: * Pros: This approach uses the `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. * Cons: The `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). 2. **Using a Set**: * Pros: This approach uses a built-in JavaScript data structure that provides fast lookups and efficient set operations. It's also concise and readable. * Cons: The `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.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn vs custom2
Lodash uniq vs Object unique keys vs Set
Javascript unique string array
Array of strings, null, and ints lodash uniq vs set
Comments
Confirm delete:
Do you really want to delete benchmark?