Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Javascript unique string array
(version: 0)
Benchmark.js
Create a unique array of strings from a possible non-unique array.
Comparing performance of:
Using an object vs Using a Set
Created:
3 years ago
by:
Registered User
Go 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; }, {}));
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Using a Set
3.8M/s
Using an object
3.2M/s
View exact numbers
Test name
Executions per second
✓
Using a Set
3,800,777 Ops/sec
Using an object
3,243,161 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this benchmark. **Benchmark Overview** This benchmark compares two approaches to remove duplicate strings from an array: using an object and using a Set. The goal is to create a unique array of strings from a possible non-unique array. **Options Compared** Two options are compared: 1. **Using an object**: This approach uses the `Object.keys()` method to get an array of unique keys from an object, where each key is a string. The `reduce()` method is used to iterate over the original array and add each item to the object only if it's not already present. 2. **Using a Set**: This approach uses the `Set` data structure to automatically eliminate duplicates. It converts the array to a Set using `Array.from(new Set(nonUnique))`, which returns an array of unique strings. **Pros and Cons** **Using an Object:** Pros: * Can handle non-unique arrays with arbitrary key values. * Does not require any additional libraries or imports. Cons: * Can be slower than the Set approach, especially for large datasets, since it involves creating an object and iterating over its keys. * May have performance issues if the array is very large, as it requires more memory to store the object. **Using a Set:** Pros: * Fast and efficient, as Sets are optimized for lookup and uniqueness. * Does not require any additional libraries or imports. Cons: * Requires the `Set` data structure, which may not be available in older browsers or environments. * May not work correctly if the array contains non-string values or objects with non-unique properties. **Other Considerations** When choosing between these two approaches, consider the specific requirements of your use case. If you need to handle non-unique arrays with arbitrary key values, using an object might be a better choice. However, if performance is critical and you're working with large datasets, using a Set could be a better option. **Library/Dependencies** Neither of these approaches requires any additional libraries or dependencies beyond what's built into JavaScript. However, the `Set` data structure is optimized for specific browsers and environments, so it may not work correctly in older or less-capable browsers. **Special JS Feature/Syntax** The `reduce()` method and the `Object.keys()` method are part of the standard JavaScript syntax. However, some features like array destructuring (`Array.from(new Set(nonUnique))`) might be more recent additions to the language, introduced in ECMAScript 2015 (ES6).
Related benchmarks
Lodash uniq vs Object unique keys vs Set
Array of strings, null, and ints lodash uniq vs set
Comments
Confirm delete:
Do you really want to delete benchmark?