Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object VS new Set VS Includes
(version: 0)
Comparing performance of:
new Set vs Object vs Includes
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = []; var b = {} for (var i = 0; i < 5000; i++) { a.push(i); b[i] = `${i}`; } var set = new Set([...a]);
Tests:
new Set
set.has(4500)
Object
b[4500]
Includes
a.includes(4500)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
new Set
Object
Includes
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):
Let's dive into the explanation of the benchmark. **What is being tested?** The benchmark measures the performance of three different approaches to check if an element exists in a collection: 1. `new Set()` - Using a new set data structure to store unique elements and then checking if an element exists using the `has()` method. 2. Object literal (`{}`) - Creating an object literal with a large number of properties (5000 in this case) and then accessing an element using its key (`b[4500]`). 3. Array `.includes()` method - Using the `.includes()` method to check if an element exists in the array `a`. **Options compared** The benchmark compares the performance of these three approaches: * `new Set()`: A new set data structure is used to store unique elements, and then the `has()` method is used to check if an element exists. * Object literal: An object literal with a large number of properties is created, and then the property value at index 4500 is accessed using `b[4500]`. * Array `.includes()` method: The array `a` is populated with elements and then the `.includes()` method is used to check if an element exists. **Pros and cons of each approach** Here's a brief summary: 1. **new Set()**: * Pros: + Fast lookups (O(1) time complexity) + Unique elements are stored, which can be beneficial for certain use cases * Cons: + Additional memory usage due to the set data structure + Creation of a new set object may incur overhead 2. **Object literal**: * Pros: + No additional memory overhead compared to arrays or sets + Can be used for other purposes beyond simple element existence checks * Cons: + Slow lookups (O(1) time complexity, but can be slower in practice due to property access) + Property access may incur additional overhead due to the object's size and complexity 3. **Array `.includes()` method**: * Pros: + Fast lookups (O(n) time complexity, where n is the number of elements in the array) + No additional memory overhead * Cons: + Slower for large arrays due to the linear search algorithm used by `.includes()` + Less efficient than `new Set()` or object literal for very large datasets **Library and its purpose** The `Set` data structure is a built-in JavaScript library that provides an efficient way to store unique elements. It's implemented as a hash table, which allows for fast lookups (O(1) time complexity). **Special JS feature or syntax** None mentioned in the benchmark definition. Now, let's talk about other alternatives: * Using `in` operator instead of `.includes()` method: This can be slightly faster than `.includes()` but may incur additional overhead due to property access. * Using a library like Lodash's `includes()` function: These libraries often provide optimized implementations for common array operations. These are just some of the options that could be considered when performing similar benchmarks. The best approach depends on the specific use case and requirements.
Related benchmarks:
set vs array iteration
set vs array iteration 100k elements
set vs array iteration new
set vs array iteration new new
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?